Passing data between views not working

I know this question was asked about a 100 times but I just can't figure out what I'm doing wrong.

This ist my second iOS App. I want to modify a Label in my ViewControllerB by pressing a Button in my ViewControllerA.

  • I made a Segue in Storyboard from my Button ( ViewControllerA ) to my ViewControllerB and called it "TestSegue".
  • Wrote #import "ViewControllerB.h in my ViewControllerA.h
  • I wrote this Code in my ViewControllerA.m :
  • -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"TestSegue"]){
    
    ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
    controller.TestLabel.text = @"TEST!";}}
    

    In my ViewControllerB nothing happens with the TestLabel...

    Can anybody see what I did wrong?

    Thanks in advance

    Edit: Works now thanks to rpledge.

    New Code:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    if([segue.identifier isEqualToString:@"TestSegue"]){
    
    ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
    controller.TestString = @"TEST!";}}
    

    It'l likely because the TextLabel UI element isn't created until viewDidLoad is run, which will happen after prepareForSegue:

    I don't like exposing UI elements for views publicly anyway, I would suggest you add an @property for your NSString* to the destination view controller then set the UILabel.text in viewDidLoad:


    Don't assign it to the control, but to an instance variable.

    @property (nonatomic) NSString *var;
    

    And then in the viewDidLoad assign this to the control.

    链接地址: http://www.djcxy.com/p/61274.html

    上一篇: 如何使用iOS6风格嵌入seque来配置嵌入式视图控制器

    下一篇: 在视图之间传递数据不起作用