简体   繁体   English

在ViewController中以编程方式使用NavigationController

[英]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.

相关问题 以编程方式导航到另一个故事板上的navigationcontroller和tabbarcontroller内的viewcontroller - Navigate programmatically to viewcontroller inside navigationcontroller & tabbarcontroller on another storyboard 以编程方式呈现ViewController的嵌入NavigationController - Presenting a ViewController's embedding NavigationController programmatically 从推出的ViewController内部中删除Modal NavigationController - Dismiss Modal NavigationController from pushed ViewController inside 在TabBarController内部的NavigationController中从(Table)ViewController呈现模态ViewController - Presenting Modal ViewController from (Table)ViewController inside a NavigationController inside a TabBarController 快速地以编程方式创建NavigationController时,ViewController出现了多次 - ViewController appears mulitple times when creating a navigationController programmatically in swift 以编程方式将NavigationController添加到指定viewController中,而无需故事板快速 - Add navigationController to specif viewController programmatically without storyboard swift 以编程方式单击UITabBarItem时如何将ViewController推送到NavigationController? - How to push ViewController to NavigationController when UITabBarItem is clicked programmatically? 从UIViewController切换到navigationController内部的其他ViewController - Switch from UIViewController to other ViewController which is inside navigationController 从显示的屏幕显示导航控制器内的 ViewController:ios,swift - showing ViewController inside navigationController from a presented screen : ios, swift 如何从嵌入在TabController中的NavigationController内的ViewController访问tabBarItem? - How to access tabBarItem from ViewController inside of NavigationController embedded in TabController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM