新手问题 可以直接继承 UINavigationController 吗

twm · November 20, 2014 · Last by PrideChung replied at November 21, 2014 · 2667 hits
lass ViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()

        self.title = "xxx"

        self.navigationItem.title = "xxx"
//        self.navigationBar.backgroundColor = UIColor.orangeColor()

        let navBarItem = UINavigationItem(title: "xxx")

//        self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: "xx")
//        self.navigationBar.pushNavigationItem(navBarItem, animated: true)
//        self.navigationBar.setItems([navBarItem], animated: false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

无论是通过 self.title 还是 navigationItem.title 设置标题都无效,包括下面注释掉的部分,设置都无效。

但 navigationBar.backgroundColor 有颜色效果。 我想问的是,既然无法通过继承来设置这些元素,那么为什么要将这个类开放让开发者可继承?

我的目的是一个 UINavBar 的 first item 是上述 ViewController,因为其本身是一个 NavigationViewController 了,所以就不用包装了,比如 let navigation = UINavigationController(rootViewController: ViewController)

还有就是继承自 UITabBarController 的 ViewController 却可以直接设置其 TabBarItem。为什么继承上述 ViewController 缺不行?

你要设嵌套在UINavigationController里面的UIViewController 的title属性,而不是UINavigationController自己的title属性

#1 楼 @PrideChung 嗯,但继承自 UITabBarController 的控制器缺可以设置设置其属性,并且生效?

不太明白你的意思

You need to Sign in before reply, if you don't have an account, please Sign up first.