iOS:以编程方式呈现视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16152746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:17:23  来源:igfitidea点击:

iOS: present view controller programmatically

iosobjective-cuiviewcontrolleruistoryboardpresentviewcontroller

提问by Salieh

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

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

This is my code:

这是我的代码:

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

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

此代码转到另一个,UIViewController但另一个控制器是空的。我一直在使用故事板,但现在我需要在代码中完成。

回答by Tim

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

如果您使用的是故事板,您可能不应该使用allocinit创建新的视图控制器。相反,看看你的故事板,并找到SEGUE要执行; 它应该有一个唯一的标识符(如果没有,您可以在右侧栏中设置一个)。

Once you've found the identifier for that segue, send your currentview controller a -performSegueWithIdentifier:sendermessage:

找到该 segue 的标识符后,向当前的视图控制器发送一条-performSegueWithIdentifier:sender消息:

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

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

这将导致故事板实例化一个 AddTaskViewController 并以您为该 segue 定义的方式呈现它。



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 提供某种用户界面。这样做的最常见方法是使用 nib 初始化控制器:init您将调用-initWithNibName:bundle:并提供包含添加任务 UI 的 .xib 文件的名称,而不是仅调用:

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.)

(还有其他(不太常见的)方法可以获取与新视图控制器关联的视图,但这可能会给您带来最少的工作麻烦。)

回答by Leg1oneR

If you are using Storyboard and your "add" viewController is in storyboard then set an identifier for your "add" viewcontroller in settings so you can do something like this:

如果您正在使用 Storyboard 并且您的“添加”视图控制器在故事板中,那么在设置中为您的“添加”视图控制器设置一个标识符,以便您可以执行以下操作:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" 
                                                     bundle:nil];
AddTaskViewController *add = 
           [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];

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

if you do not have your "add" viewController in storyboard or a nib file and want to create the whole thing programmaticaly then appDocs says:

如果您在故事板或 nib 文件中没有“添加”viewController 并且想要以编程方式创建整个内容,那么 appDocs 说:

If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property.

如果您无法在故事板或 nib 文件中定义视图,请覆盖 loadView 方法以手动实例化视图层次结构并将其分配给视图属性。

回答by Rajesh Gupta

You need to set storyboard Id from storyboard identity inspector

您需要从故事板身份检查器中设置故事板 ID

 AddTaskViewController *add=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_id"];
 [self presentViewController:add animated:YES completion:nil];

回答by Sahidul Islam

The best way is

最好的办法是

AddTaskViewController * add = [self.storyboard instantiateViewControllerWithIdentifier:@"addID"];
[self presentViewController:add animated:YES completion:nil];

回答by klone

your code :

你的代码:

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

this code can goes to the other controller , but you get a new viewController , not the controller of your storyboard, you can do like this :

此代码可以转到另一个控制器,但是您会得到一个新的 viewController ,而不是故事板的控制器,您可以这样做:

AddTaskViewController *add = [self.storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardID"];
[self presentViewController:add animated:YES completion:nil];

回答by MNIDR

Try the following:

请尝试以下操作:

NextViewController *nextView = [self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
[self presentViewController:nextView animated:YES completion:NULL];

回答by Esqarrouth

Here is how you can do it in Swift:

以下是您在 Swift 中的操作方法:

let vc = UIViewController()
self.presentViewController(vc, animated: true, completion: nil)

回答by Oshitha Wimalasuriya

Try this code:

试试这个代码:

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

回答by MNIDR

LandingScreenViewController *nextView=[self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
[self presentViewController:nextView animated:YES completion:^{}];