Swift:未调用自定义UINavigationController

时间:2017-11-20 21:21:31

标签: swift xcode storyboard navigationcontroller

我想以编程方式更改导航控制器的根控制器:

import UIKit
import Foundation

class NavigationController : UINavigationController {
    override init(rootViewController : UIViewController) {
        print("TEST")
        super.init(rootViewController : rootViewController)
    }

    override init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?) {
        print("TEST2")
        super.init(navigationBarClass : navigationBarClass, toolbarClass : toolbarClass)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

我将NavigationController类设置为故事板中导航控制器的自定义类

控制台未显示我的测试输出。我做错了什么,如何在这里更改根控制器?

2 个答案:

答案 0 :(得分:1)

如果您从NavigationController实例化自定义Storyboard,则表示您正在调用此方法:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

要在控制台中查看您的打印件,您必须以编程方式启动它,例如:

let navVC = NavigationController(rootViewController: yourVC)

答案 1 :(得分:0)

知道了:

import UIKit
import Foundation

class NavigationController : UINavigationController {
    override init(rootViewController : UIViewController) {
        super.init(rootViewController : rootViewController)
    }

    override init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?) {
        super.init(navigationBarClass : navigationBarClass, toolbarClass : toolbarClass)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "new_main_view") as! ViewController
        self.setViewControllers([newViewController], animated: false)
    }
}

这是为用户提供同一视图的两个“主题”的推荐方法吗?