iOS:以编程方式呈现视图控制器 [英] iOS: present view controller programmatically

查看:105
本文介绍了iOS:以编程方式呈现视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用presentViewController:animated:completion:方法转到另一个视图控制器.

I'm using the presentViewController:animated:completion: method to go to another view controller.

这是我的代码:

AddTaskViewController *add = [[AddTaskViewController alloc] init];
[self presentViewController:add animated:YES completion:nil];

此代码转到另一个UIViewController,但是另一个控制器为空.我一直在使用情节提要,但现在我需要用代码来完成.

This code goes to the other UIViewController but the other controller is empty. I've always been using storyboards but now I need this to be done in code.

推荐答案

如果使用情节提要,则可能不应该使用allocinit创建新的视图控制器.相反,请查看您的情节提要,找到要执行的 segue ;它应该具有唯一的标识符(如果没有,则可以在右侧栏中设置一个).

If you're using a storyboard, you probably shouldn't be using alloc and init to create a new view controller. Instead, look at your storyboard and find the segue that you want to perform; it should have a unique identifier (and if not, you can set one in the right sidebar).

找到该序列的标识符后,向 current 视图控制器发送-performSegueWithIdentifier:sender消息:

Once you've found the identifier for that segue, send your current view controller a -performSegueWithIdentifier:sender message:

[self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];

这将使情节提要板实例化AddTaskViewController并以您为该脚本定义的方式呈现它.

This will cause the storyboard to instantiate an AddTaskViewController and present it in the way that you've defined for that segue.

另一方面,如果您根本不使用情节提要,那么您需要为AddTaskViewController提供某种用户界面.最常见的方法是使用nib初始化控制器:您将调用-initWithNibName:bundle:而不是仅调用init并提供包含添加任务UI的.xib文件的名称:

If, on the other hand, you're not using a storyboard at all, then you need to give your AddTaskViewController some kind of user interface. The most common way of doing so is to initialize the controller with a nib: instead of just calling init, you'll call -initWithNibName:bundle: and provide the name of a .xib file that contains your add-task UI:

AddTaskViewController *add = [[AddTaskViewController alloc]
                              initWithNibName:@"AddTaskView" bundle:nil];
[self presentViewController:add animated:YES completion:nil];

(还有其他(较不常见)的方法来获取与新视图控制器关联的视图,但这可能使您工作起来的麻烦最少.)

(There are other (less common) ways of getting a view associated with your new view controller, but this will probably present you the least trouble to get working.)

这篇关于iOS:以编程方式呈现视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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