简体   繁体   English

快速以编程方式打开viewController

[英]Opening viewController programmatically in swift

I want to open new UIViewController on the button through code. 我想通过代码在按钮上打开新的UIViewController。 I have already made the controller in storyboard and just want to link it and also I don't want to use XIB interface or nibName? 我已经在情节提要中创建了控制器,只想链接它,也不想使用XIB接口或nibName?

To open New view controller you need to write this line in button click event: 要打开New view controller,您需要在按钮单击事件中编写以下行:

self.performSegueWithIdentifier("GoToViewController", sender:self)

To link up with new view controller follow this steps: 要与新的视图控制器链接,请执行以下步骤:

  • Select New view controller from storyboard & right click on it 从情节提要板上选择“新视图控制器”,然后右键单击它

  • You will find Dark dray popup will appear (see image ) 您会发现将出现Dark dray弹出窗口(参见图片)

  • Now under Presenting Segues select Present Modally & drag to the View controller from where you need to open up this view controller & link it 现在在Presentation Segues下,选择Present Modally并拖到需要打开此View Controller并将其链接的View Controller上。

  • You will find new segue created under New view controller 您会在“新视图”控制器下找到创建的新序列

  • Now select that segue go to inspector & copy the identifier or rename & copy it 现在选择该segue去检查器并复制标识符或重命名并复制它

  • Use that identifier in above line 在上面的行中使用该标识符

Hope it will work. 希望它能工作。

在此处输入图片说明

self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier("userProfileController") as UIViewController, animated: true)

我想将我的视图控制器添加到导航控制器,这也有效

对于Swift 4.2,请尝试以下代码:

self.navigationController!.pushViewController(self.storyboard!.instantiateViewController(withIdentifier: "inventoryViewController") as UIViewController, animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM