ios - 在没有 prepareForSegue 的情况下推送到另一个 View Controller

标签 ios objective-c uiviewcontroller uistoryboardsegue pushviewcontroller

如何在没有 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/

相关文章:

ios - 如何从不在 Storyboard 中但以编程方式创建的 View Controller 呈现 "main" Storyboard 中的 View Controller ?

ios - 为什么 NSCalendar 的 compareDate 似乎需要设置 UTC 时区才能正常工作?

ios - 检测连续/重复摇晃

ios - 如何从 iOS 中的用户输入中删除数字?

objective-c - iOS TabBar 应用程序中的全局变量

ios - iOS中多个UIView的UIView继承

ios - iPhone 应用审查和 SHA1

ios - 无限滚动或其他一些技术?

iphone - 在 iOS 上运行无限循环动画

iphone - 在许多 UIVIewController 之间传递一个变量