根据模式/情况更改NavigationBar.title?

时间:2018-11-15 09:13:13

标签: ios swift uinavigationcontroller switch-statement navigationbar

我有viewController,它具有三种情况/三种模式。

它具有“创建新人员”模式,“显示人员”模式以及最后的“编辑人员”模式。这一切都很好-但是我似乎无法根据模式/情况来设置navigationBar.title。

以下是相关代码:

private func modeUpdate() {
   let createNewPerson:[NSAttributedString.Key: Any] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue):UIColor.blue, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name:"Create New Person", size: 17)!]
   let showPerson:[NSAttributedString.Key: Any] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue):UIColor.blue, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name:"", size: 17)!]
   let editPerson:[NSAttributedString.Key: Any] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue):UIColor.blue, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name:"Edit Person", size: 17)!]


    switch mode {
    case .create:
        actionButton.title = "Save"
        personName.isEditable = true
        navigationController?.navigationBar.titleTextAttributes = createNewPerson
        question.isEditable = false
        personAnswer.isEditable = true
        extraIdentifier.isEditable = false
        extraIdentifierAnswer.isEditable = true

    case .show:
        actionButton.title = "Edit"
        navigationController?.navigationBar.titleTextAttributes = showPerson
        personName.isEditable = false
        question.isEditable = false
        personAnswer.isEditable = false
        extraIdentifier.isEditable = false
        extraIdentifierAnswer.isEditable = false

    case .edit:
        actionButton.title = "Save"
        navigationController?.navigationBar.titleTextAttributes = editPerson
        personName.isEditable = true
        question.isEditable = false
        personAnswer.isEditable = true
        extraIdentifier.isEditable = false
        extraIdentifierAnswer.isEditable = true
    }
}

全部编译,但是当我在设备上输入以下三种情况之一时,我得到:“线程1:致命错误:意外地发现nil,同时展开一个可选值”在“让createNewPerson:[NSAttributed。 ...'

有人知道为什么会这样吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试这个self.navigationItem.title = "text"