简体   繁体   中英

Passing data to UIViewController when using modal

I have two view controllers, MainViewController and SecondViewController.

In my main view controller, I have data I pass to the second view controller like so:

-(IBAction)shareToSocial:(id)sender {
    SecondViewController *view2 = (SecondViewsController *)[storyboard instantiateViewControllerWithIdentifier:@"PushToNext"];
    view2.secTitle = self.MainTitle;
    [self.navigationController setModalPresentationStyle: UIModalPresentationCurrentContext];
    [self setModalPresentationStyle:UIModalPresentationCurrentContext];
}

Where both secTitle and MainTitle are NSStrings for each controller. From what I've read, by setting view2.secTitle to self.MainTitle, when SecondViewController pops in I my view2.secTitle will have the same value as MainTitle. So if MainTitle is "Hello World" then I open secondViewController, secTitle would also be "Hello World". However, I've been getting null with secTitle even though I am setting the data for it.

If I use pushViewController instead of modal, the values are passed between the two controllers fine. So I'm not sure if I'm doing anything wrong here exactly.

I've also tried using [view2 setSecTitle:MainTitle] to no avail.

There's not much code on my SecondViewController but here's how it looks:

-(IBAction)showMessage:id(sender){
     NSLog(@"test: %@", self.secTitle);
}

-(IBAction)closeMessage:id(sender){
    [self dismissViewControllerAnimated:YES completion:nil];
}

Also, I've noticed that when I used dismissViewControllerAnimated on SecondViewController to close it and go back to the MainViewController, a black empty screen shows for a few seconds before showing the Main View and touch events on MainViewController is not detected anymore.

Please help.

Could you use performSegueWithIdentifier and prepareForSegue? If you've got your views in your storyboard and set up a segue between them, given it an identifier and set the style to modal you should be able to do this:

-(IBAction)shareToSocial:(id)sender {
    [self performSegueWithIdentifier:@"modelToNextSegue" sender:self];
}

Then in prepareForSegue method you pass your values:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SecondViewController *view2 = [segue destinationViewController];
    view2.secTitle = self.MainTitle;
}

Try this:

SecondViewController *view2 = (SecondViewsController *)[storyboard instantiateViewControllerWithIdentifier:@"PushToNext"];
view2.secTitle = self.MainTitle;
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:view2];
[nav setModalPresentationStyle: UIModalPresentationCurrentContext];
[self setModalPresentationStyle:UIModalPresentationCurrentContext];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM