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

查看:89
本文介绍了推送到另一个视图控制器没有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一个故事板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中有自己的故事板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天全站免登陆