1

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.

4

2 回答 2

1

你可以使用 performSegueWithIdentifier 和 prepareForSegue 吗?如果您在故事板中有您的视图并在它们之间设置了一个segue,给它一个标识符并将样式设置为模态,您应该能够做到这一点:

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

然后在 prepareForSegue 方法中传递您的值:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SecondViewController *view2 = [segue destinationViewController];
    view2.secTitle = self.MainTitle;
}
于 2013-11-08T12:24:17.250 回答
0

尝试这个:

SecondViewController *view2 = (SecondViewsController *)[storyboard instantiateViewControllerWithIdentifier:@"PushToNext"];
view2.secTitle = self.MainTitle;
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:view2];
[nav setModalPresentationStyle: UIModalPresentationCurrentContext];
[self setModalPresentationStyle:UIModalPresentationCurrentContext];
于 2013-11-08T09:33:29.907 回答