在没有 prepareForSegue 的情况下推送到另一个视图控制器 [英] push to another view controller without prepareForSegue

查看:12
本文介绍了在没有 prepareForSegue 的情况下推送到另一个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有 prepareForSegue 的情况下推送到另一个视图控制器?

How can you push to another view controller without 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];        
}

推荐答案

如果你想以编程方式调用推送 segue,你可以在 Interface Builder 中给 segue 一个storyboard id",然后你可以:

If you want to programmatically invoke a push segue, you give the segue a "storyboard id" in Interface Builder and then you can:

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

或者,如果您不想执行 segue,您可以实例化目标视图控制器,然后手动推送到该视图控制器.您需要做的就是确保目标视图控制器在 Interface Builder 中有自己的storyboard id",然后您可以:

Alternatively, if you don't want to perform the segue, you can instantiate the destination view controller and then manually push to that view controller. All you need to do is to make sure that the destination view controller has its own "storyboard id" in Interface Builder, then you can:

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

您说的是推送"(因此我使用了上面的 pushViewController).如果你真的想呈现一个模态视图控制器",那么第二行是:

You said "push" (and hence I used pushViewController above). If you really meant to "present a modal view controller", then that second line is:

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

如您所见,您不必使用 prepareForSegue 推送到新场景.如果您想将信息传递给目标视图控制器,您只能使用 prepareForSegue.否则不需要.

As you can see, you don't have to use prepareForSegue to push to new scene. You only use prepareForSegue if you want to pass information to the destination view controller. Otherwise it is not needed.

显然,如果您不使用故事板(例如,您使用的是 NIB),那么过程就完全不同了.但我假设您没有使用 NIB,因为 prepareForSegue 在该环境中不适用.但如果您使用的是 NIB,则如下所示:

Clearly, if you're not using storyboards (e.g., you're using NIBs), then the process is entirely different. But I assume you're not using NIBs because prepareForSegue is not applicable in that environment. But if you were using NIB, it would be as follows:

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

这篇关于在没有 prepareForSegue 的情况下推送到另一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆