【发布时间】:2014-06-24 09:29:47
【问题描述】:
我以编程方式在我的 Superview 中添加了 UINavigationBar,名为 BaseViewController,它继承了 UIViewController。但是当我尝试为 UINavigationBar 设置标题时。我收到名为 fatal error: Can't unwrap 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)
}
}
【问题讨论】:
-
如果可选对象为 nil,则不能解包。这就是你得到错误的原因。始终验证对象为零。正如 Rockey 建议的那样尝试 self.title
标签: uinavigationbar swift ios8