我正在尝试构建一个集成了聊天选项的应用程序。我的对话视图控制器(一个人的所有聊天都显示在具有多个单元格的 tableView 中)嵌入到导航控制器以及单个聊天视图控制器(您只与一个人聊天)中。
要从对话视图控制器转到单个聊天视图控制器,我正在实现以下代码:
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)
但是,_vc.title 没有显示在导航栏上。我该如何解决这个问题?
答案 0 :(得分:0)
_vc.navigationItem.title = "John Smith"
使用导航项设置标题。
答案 1 :(得分:0)
您需要先创建一个 UINavigationController 实例:
UINavigationController *navController = [self.storyboard instantiateViewControllerWithIdentifier:@"Viewe"];
ViewController2 *vc = (ViewController2 *) [navController topViewController];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
vc.title = @"hello";
并展示该 navController 实例。
答案 2 :(得分:0)
您应该首先展示一个 VC,如果您正在考虑添加一个 VC,那么它会是这样的。
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)
并在 SecViewController 的 viewDidLoad
中给出
title = "John Smith"
。这将帮助您添加标题。