Best way to pass variables between views

前端 未结 4 1394
走了就别回头了
走了就别回头了 2020-12-21 12:56

I am very new to xcode & Objective-C (having come from PHP) i have started to play around and am finding it very hard to pass variables between views this is what i have

4条回答
  •  春和景丽
    2020-12-21 13:31

    Take this in .h file in SecondViewController

    NSString *strABC;
    

    Make below function in SecondViewController.m

    -(void)setString:(NSString *)strEntered{
        strABC=strEntered;
    }
    

    Now In First view controller do like this:

    SecondViewController.m *objSecond = [[SecondViewController.m] initwithNibName:@"secondView.xib" bundle:nil];
    
    [objSecond setString:@"Comment Controller"];
    [self.navigationController pushViewController:objSecond animated:YES];
    [objSecond release];
    

    Now, In secondViewController viewWillAppear Method write this.

    -(void)viewWillAppear:(BOOL)animated{
          lblUserInput.text = strABC;
    }
    

    Please check spelling mistakes as I hand written this. Hope this help.

    If you are not using navigationContoller then you can do something like this.

    SecondViewControler *objSecond = [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil];
    [objSecond setUserInput:txtUserInput.text];
    [objSecond viewWillAppear:YES];
    [self.view addSubview:objSecond];
    [objSecond release];
    

提交回复
热议问题