推到另一个视图控制器没有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]; } 

如果你想以编程方式调用push segue,你可以在界面生成器中给segue一个“storyboard id”,然后你可以:

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

或者,如果您不想执行segue,则可以实例化目标视图控制器,然后手动推送到该视图控制器。 您只需要确保目标视图控制器在Interface Builder中具有自己的“storyboard 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]; 

[self presentViewController:viewController animated:YES completion:nil]; 是不需要的,因为segue会使用您自动select的转换推送目标视图控制器。

如果您不想使用segue过程,则需要手动推送视图控制器:

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

但要确保先删除故事板中的赛段。

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