在iOS8 Swift中更改UINavigationBar标题

Ash*_*han 8 uinavigationbar swift ios8

我以编程方式在我的Superview中添加了UINavigationBar,名为BaseViewController,它继承了UIViewController.但是当我尝试为UINavigationBar设置标题时.我收到称为致命错误的错误:无法解包Optional.None

以下是我的快速课程.如何解包和更改UINavigationBar标题

import UIKit

class BaseViewConroller: UIViewController {
    var navBar:UINavigationBar=UINavigationBar()

    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBarToTheView()
        // Do any additional setup after loading the view.
        navBar.topItem.title="test test"
    }

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

    func setNavBarToTheView()
    {
        navBar.frame=CGRectMake(0, 0, 320, 50)
        navBar.backgroundColor=(UIColor .blackColor())
        self.view .addSubview(navBar)
    }

}
Run Code Online (Sandbox Code Playgroud)

Roc*_*era 29

更换你navBar.topItem.title="test test"self.title="test test".干杯!!