iOS:以编程方式显示视图控制器

iOS:以编程方式显示视图控制器,ios,objective-c,uiviewcontroller,uistoryboard,presentviewcontroller,Ios,Objective C,Uiviewcontroller,Uistoryboard,Presentviewcontroller,我正在使用presentViewController:animated:completion:方法转到另一个视图控制器 这是我的代码: AddTaskViewController *add = [[AddTaskViewController alloc] init]; [self presentViewController:add animated:YES completion:nil]; 此代码转到另一个UIViewController,但另一个控制器为空。我一直在使用故事板,但现在我需要在

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

这是我的代码:

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

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

如果使用故事板,可能不应该使用
alloc
init
来创建新的视图控制器。相反,看看你的故事板,找到你想要表演的片段;它应该有一个唯一的标识符(如果没有,您可以在右边栏中设置一个)

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

[self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];
这将导致情节提要实例化AddTaskViewController,并以您为该序列定义的方式呈现它


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

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

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

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

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

[self presentViewController:add 
                   animated:YES 
                 completion:nil];
如果您在故事板或nib文件中没有“添加”viewController,并且希望通过编程创建整个内容,那么appDocs会说:

如果无法在序列图像板或nib文件中定义视图,请重写loadView方法以手动实例化视图层次结构并将其指定给视图属性


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

let vc = UIViewController()
self.presentViewController(vc, animated: true, completion: nil)
您的代码:

 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];
请尝试以下操作:

NextViewController *nextView = [self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
[self presentViewController:nextView animated:YES completion:NULL];
请尝试以下代码:

[self.navigationController presentViewController:controller animated:YES completion:nil];
最好的办法是

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

您需要从情节提要标识检查器设置情节提要标识

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

这不是一个Xcode问题。另外,我无法推断你所说的“另一个控制器什么都不是”是什么意思,所以我把它原封不动地保留了下来。如果它与一个按钮结合在一起,它的工作原理就是。那么,在非情节提要场景中,如何“取消呈现”新的视图控制器呢?我在谷歌上搜索,试图找到类似于unwindForSegue的东西:towardsViewController:事实上,我刚刚找到了“dismissViewController”,我认为这是正确的答案。谢谢。对我有用。这只是加载了一个空白的UIViewControllero,当然了。您必须将其替换为自己的viewcontroller,或在UIViewController的视图中添加某种颜色或对象。如果答案不好,但却是答案(就像这样),则不建议删除:向下投票!看见这是一个答案。你可能不同意,但这是试图回答这个问题。
 AddTaskViewController *add=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_id"];
 [self presentViewController:add animated:YES completion:nil];