简体   繁体   English

在导航控制器之间导航时导航栏不显示标题

[英]Navigation Bar not showing Title when navigating between navigation controllers

I am trying to build an app with chat options integrated.我正在尝试构建一个集成了聊天选项的应用程序。 My conversations view controller(where all the chats of one person are displayed in a tableView with multiple cells) is embedded in a navigation controller as well as the single chat view controller(where you are chatting with only one person).我的对话视图控制器(其中一个人的所有聊天都显示在具有多个单元格的 tableView 中)嵌入在导航 controller 以及单个聊天视图控制器(您只与一个人聊天)中。

To go from the conversations view controller to the single chat view controller I am implementing this code:从对话视图 controller 到 go 到单个聊天视图 controller 我正在实现此代码:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let _vc = storyboard.instantiateViewController(withIdentifier: "myNavigationController") as! UINavigationController
        _vc.title = "John Smith"
        _vc.modalPresentationStyle = .fullScreen
        present(_vc, animated: true, completion: nil)

However, the _vc.title is not showing on the navigation bar.但是,_vc.title 没有显示在导航栏上。 How could I fix this?我该如何解决这个问题?

_vc.navigationItem.title = "John Smith"

Set title using navigation item.使用导航项设置标题。

You need to create an instance of UINavigationController first:您需要先创建一个 UINavigationController 的实例:

 UINavigationController *navController = [self.storyboard instantiateViewControllerWithIdentifier:@"Viewe"];
ViewController2 *vc = (ViewController2 *) [navController topViewController];

navController.modalPresentationStyle = UIModalPresentationFullScreen;

vc.title = @"hello";

and present that navController instance.并呈现该 navController 实例。

There should be a VC that you are presenting first of all and incase you are considering of adding a VC then it goes something like this.首先应该有一个 VC,如果你正在考虑添加一个 VC,那么它会像这样。

In Swift:在 Swift 中:


 let sb = UIStoryboard(name: "Main", bundle: nil)
 guard let vc = sb.instantiateViewController(withIdentifier: "SecViewController") as? SecViewController else { return }
 let navController = UINavigationController(rootViewController: vc)
 navigationController?.modalPresentationStyle = .fullScreen
 present(navController, animated: true)

And in the viewDidLoad of SecViewController give title = "John Smith" .并在 SecViewController 的viewDidLoad中给出title = "John Smith" This will help you add title.这将帮助您添加标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM