38

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

这是我的代码:

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

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

4

9 回答 9

54

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

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

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

这将导致情节提要实例化一个 AddTaskViewController 并以您为该 segue 定义的方式呈现它。


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

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

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

于 2013-04-22T17:11:52.177 回答
31

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

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

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

如果您在情节提要或 nib 文件中没有“添加”视图控制器,并且想要以编程方式创建整个内容,那么 appDocs 会说:

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

于 2013-04-22T18:08:05.433 回答
2

您需要从情节提要身份检查器设置情节提要 ID

 AddTaskViewController *add=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_id"];
 [self presentViewController:add animated:YES completion:nil];
于 2017-10-16T14:19:49.213 回答
1

你的代码:

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

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

AddTaskViewController *add = [self.storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardID"];
[self presentViewController:add animated:YES completion:nil];
于 2015-11-30T06:05:03.007 回答
1

最好的方法是

AddTaskViewController * add = [self.storyboard instantiateViewControllerWithIdentifier:@"addID"];
[self presentViewController:add animated:YES completion:nil];
于 2017-10-12T03:09:16.923 回答
0

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

let vc = UIViewController()
self.presentViewController(vc, animated: true, completion: nil)
于 2015-11-30T01:52:57.760 回答
0

尝试以下操作:

NextViewController *nextView = [self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
[self presentViewController:nextView animated:YES completion:NULL];
于 2016-02-16T06:53:54.243 回答
0

试试这个代码:

[self.navigationController presentViewController:controller animated:YES completion:nil];
于 2017-02-28T09:48:48.097 回答
-1
LandingScreenViewController *nextView=[self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
[self presentViewController:nextView animated:YES completion:^{}];
于 2016-03-28T06:26:04.010 回答