如何在没有 prepareForSegue
的情况下推送到另一个 View Controller ?
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];
}
最佳答案
如果您想以编程方式调用推送转场,您可以在 Interface Builder 中为转场提供一个“ Storyboard ID”,然后您可以:
[self performSegueWithIdentifier:"pushToMyVC" sender:self];
或者,如果您不想执行 segue,您可以实例化目标 View Controller ,然后手动推送到该 View Controller 。您需要做的就是确保目标 View Controller 在 Interface Builder 中有自己的“ Storyboard ID”,然后您可以:
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationController"];
[self.navigationController pushViewController:controller animated:YES];
你说的是“推”(因此我在上面使用了 pushViewController
)。如果您真的想“呈现模态视图 Controller ”,那么第二行是:
[self presentViewController:controller animated:YES completion:nil];
如您所见,您不必使用prepareForSegue
来推送到新场景。如果你想将信息传递给目标 View Controller ,你只需要使用 prepareForSegue
。否则不需要。
显然,如果您不使用 Storyboard(例如,您使用的是 NIB),那么过程就完全不同了。但我假设您没有使用 NIB,因为 prepareForSegue
不适用于该环境。但是,如果您使用的是 NIB,它将如下所示:
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
关于ios - 在没有 prepareForSegue 的情况下推送到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15371995/