[英]Programmatically use NavigationController inside ViewController
I have ViewController created using StoryBoards and I set it's class to be MyViewController. 我已经使用StoryBoards创建了ViewController,并将其类设置为MyViewController。
MyViewController is class that inherits from UIViewController and have some UI elements. MyViewController是从UIViewController继承并具有一些UI元素的类。 One of this elements is UIButton.
UIButton是元素之一。 I can programmatically add actions to this button as addTarget.
我可以以编程方式将操作作为addTarget添加到此按钮。
I want to to change view in NavigationController when button is clicked 我想在单击按钮时更改NavigationController中的视图
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyboard.instantiateViewController(withIdentifier: "newViewComtroller")
navigationController.pushViewController(newViewController, animated: true)
But nothing happens - why? 但是什么也没发生-为什么?
Check in your storyboard if your view controller has an embeded Navigation Controller. 在您的情节提要中检查如果您的视图控制器具有嵌入式导航控制器。 If not, you can click in your view controller, go to "Editor" on XCode's menu, "Embed in", and choose "Navigation Controller".
如果没有,您可以单击视图控制器,转到XCode菜单上的“编辑器”,“嵌入”,然后选择“导航控制器”。 After that, your view controller will be associated with a navigation controller, and you should be able to use the method "pushViewController" properly.
之后,您的视图控制器将与导航控制器关联,并且您应该能够正确使用方法“ pushViewController”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.