首页 > 解决方案 > 混合程序化和故事板。按钮单击不会在导航控制器中打开下一个 VC

问题描述

我有一个以编程方式构建的标签栏控制器。它看起来像这样:

类NewTabBarController:UITabBarController {

override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self

        createTabbar()
    }

func createTabbar() {
let deliveryViewController = storyBoard.instantiateViewController(identifier: "DeliveryViewController") as? DeliveryViewController
        deliveryViewController?.tabBarItem.image = #imageLiteral(resourceName: "icon_deliver")
        deliveryViewController?.title = "Delivery"
        planDictionary["planType"] = Constants.Mealplan.deliveryPlan
        deliveryViewController?.planDictionary = planDictionary

// Excluded other tabs and view controller creations since they are the same
}

现在这个 DeliveryViewController 在故事板中创建并嵌入到导航控制器中

它有一个按钮点击动作:

@IBAction func saveNameButton(_ sender: UIButton) {



let addressViewController = storyBoard.instantiateViewController(identifier: "AddressViewController") as? AddressViewController
                addressViewController?.planDictionary = planDictionary
                navigationController?.pushViewController(addressViewController!, animated: true)
}

当标签栏位于情节提要中时,按钮单击操作正在工作。但是在以编程方式重构之后,它不会将下一个 VC 放在导航堆栈上。

帮助将不胜感激。

标签: swiftuinavigationcontrolleruitabbarcontroller

解决方案


几乎肯定...

这一行:

let deliveryViewController = storyBoard.instantiateViewController(identifier: "DeliveryViewController") as? DeliveryViewController

正在实例化一个实例DeliveryViewController并将其设置为选项卡的视图控制器。但是您要做的是加载一个UINavigationController并制作DeliveryViewController该 NavController 的根视图控制器,并将该 NavController 设置为 Tab 项。


推荐阅读