11

你怎么能推到另一个视图控制器没有prepareForSegue?不需要推到另一个视图控制器没有prepareForSegue

myClassVC *viewController = [myClassVC alloc]; 
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"pushToMyVC" source:self destination:viewController]; 

if ([segue.identifier isEqualToString:@"pushToMyVC"]) { 
NSLog(@"logging"); 
myClassVC *viewController = (myClassVC *)[segue destinationViewController]; 
[self presentViewController:viewController animated:YES completion:nil];   
} 

回答

45

如果你想以编程方式调用推SEGUE,你给SEGUE在Interface Builder一个“故事板ID”,然后您可以:

[self performSegueWithIdentifier:"pushToMyVC" sender:self]; 

另外,如果你不想进行SEGUE ,您可以实例化目标视图控制器,然后手动推送到该视图控制器。所有你需要做的是确保目标视图控制器在Interface Builder中自己的“故事板ID”,那么您可以:

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationController"]; 
[self.navigationController pushViewController:controller animated:YES]; 

你说“推”(因此我用pushViewController以上)。如果你真正的意思“提出一个模式视图控制器”,那么下联是:

[self presentViewController:controller animated:YES completion:nil]; 

正如你所看到的,你不使用prepareForSegue推到新的场景。如果要将信息传递到目标视图控制器,则只能使用prepareForSegue。否则它不是必需的。显然,如果你不使用故事板(例如,你使用的是NIB),那么这个过程是完全不同的。但我假设你不使用NIB,因为prepareForSegue不适用于该环境。但是,如果你正在使用NIB,这将是如下:

SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
[self.navigationController pushViewController:controller animated:YES]; 
+0

这个答案非常有用! – Plagorn 2016-11-01 08:52:25

3

[self presentViewController:viewController animated:YES completion:nil];,作为SEGUE将使用您自动选择的过渡推目的地视图控制器。

如果你不想使用segue过程中,你将需要手动推视图控制器:

[self presentViewController:viewController animated:YES completion:nil];

但要确保先取出塞格斯在故事板。

0
NSString * storyboardName = @"Main"; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; 
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewControllerID"]; 
    [self presentViewController:vc animated:YES completion:nil]; 
0

在我的情况下,所有viewControllers动态编译。我不使用故事板来达到这个目的。如果这是你的情况,你可能只是实例化你想打开的viewController类,并按照下面的示例进行推送:

MyViewController* myViewController = [[MyViewController alloc] init]; 
[self.navigationController pushViewController:myViewController animated:YES]; 
相关问题