ios 以编程方式快速打开 viewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27441673/
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-31 04:00:27  来源:igfitidea点击:

Opening viewController programmatically in swift

iosswift

提问by Manu Gupta

I want to open new UIViewController on the button through code. 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?

我想通过代码在按钮上打开新的 UIViewController。我已经在故事板中制作了控制器,只想链接它,而且我不想使用 XIB 接口或 nibName?

回答by cyberlobe

To open New view controller you need to write this line in button click event:

要打开新视图控制器,您需要在按钮单击事件中编写此行:

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 )

  • Now under Presenting Segues select Present Modally & drag to the View controller from where you need to open up this view controller & link it

  • You will find new segue created under New view controller

  • Now select that segue go to inspector & copy the identifier or rename & copy it

  • Use that identifier in above line

  • 从情节提要中选择新视图控制器并右键单击它

  • 您会发现将出现 Dark dray 弹出窗口(见图)

  • 现在在 Presenting Segues 下选择 Present Modally 并从您需要打开此视图控制器并链接它的位置拖动到视图控制器

  • 你会发现在新视图控制器下创建了新的 segue

  • 现在选择那个 segue 转到检查器并复制标识符或重命名并复制它

  • 在上面的行中使用该标识符

Hope it will work.

希望它会起作用。

enter image description here

在此处输入图片说明

回答by Manu Gupta

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

I wanted to add my view controller to the navigation controller and this also worked

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

回答by Odwori

For swift 4.2, try the following code:

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

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