我需要在textField中写一些东西,当键盘中的按键返回时,我的导航栏中的全局标题会更改(对于整个应用程序)。
我被要求与代表一起执行此操作,但我没有从navigationController中找到任何委托方法来执行此操作。
答案 0 :(得分:0)
是的,你可以实现,但你必须处理每个推/出视图控制器。最好的方法是子类化导航控制器并在推送/呈现视图控制器时覆盖标题。
Swift代码:
class MyNavigationController: UINavigationController, UINavigationBarDelegate {
var navFixedTitle: String?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func pushViewController(viewController: UIViewController, animated: Bool) {
viewController.title = navFixedTitle ?? "Constant"
super.pushViewController(viewController, animated: animated)
}
//Nav Bar Delegate
func navigationBar(navigationBar: UINavigationBar, didPushItem item: UINavigationItem) {
// navigationBar.backItem?.title = "Back"
}
}
设置标题:
我已经使用了Table View,当点击不同的单元格时,导航标题会相应地设置。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let nav = self.navigationController as? MyNavigationController {
nav.navFixedTitle = sec1[indexPath.row]
}
performSegueWithIdentifier("push", sender: tableView.cellForRowAtIndexPath(indexPath))
}
<强>输出:强>