ios - 如何以编程方式呈现带有导航栏的 ViewController?
问题描述
当以这种方式呈现我的视图控制器时,我没有得到导航栏,即使视图控制器嵌入在界面构建器的导航控制器中。
if let stockVC = storyboard?.instantiateViewController(withIdentifier: "Stock") as? StockTableViewController {
stockVC.stockToDisplay = commonData.stock.filter { $0.productID.contains(product) }
// No NavigationBar with this one
navigationController?.present(stockVC, animated: true)
// No NavigationBar with this one
self.present(stockVC, animated: true)
}
我知道导航栏有效,因为如果我在情节提要中将 ViewController 设置为初始值,它会显示。
我在这里做错了什么?
解决方案
当您呈现视图控制器时,它不是当前导航堆栈的一部分。您需要创建一个包含您的 stockVC 的新导航控制器,并显示新的导航控制器:
if let stockVC = storyboard?.instantiateViewController(withIdentifier: "Stock") as? StockTableViewController {
stockVC.stockToDisplay = commonData.stock.filter { $0.productID.contains(product) }
let stockNavigationController = UINavigationController(rootViewController: stockVC)
self.present(stockNavigationController, animated: true)
}
推荐阅读
- python - 无法在项目列表中找到特定单词
- c++ - 如何在不使用 RTC 的情况下向 SD 卡添加时间戳
- html - 将背景图像添加到框元素
- postman - 邮递员:设置环境变量
- axios - Axios:使用序列化查询字符串访问 URL
- google-chrome - Google Apps 脚本不允许在 Google Workspace 中访问
- docker - 在 Windows Docker 容器中安装 Cygwin
- python-3.x - 将 Qwidgest 动态添加到 QGroupbBox
- javascript - 如何根据 React/JS 中的另一个数组修改数组的每个对象的属性?
- javascript - 我必须点击两次才能更新我的状态