作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当用户点击表格单元格时如何显示 UINavigationController?仅代码。
以下代码不起作用:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
guard let vm = (viewModel as? UICatalogVM)?.modelForRowAtIndexPath(indexPath) else {
return
}
let vc = ActivityIndicatorVC(viewModel: vm)
let nc = UINavigationController(rootViewController: self)
nc.pushViewController(vc, animated: true)
}
相反,这样的代码可以工作,但它将以模态方式显示 VC,这不是我想要做的。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
guard let vm = (viewModel as? UICatalogVM)?.modelForRowAtIndexPath(indexPath) else {
return
}
let vc = ActivityIndicatorVC(viewModel: vm)
self.showViewController(vc, sender: self)
}
最佳答案
如果您当前的 View Controller 已经在导航 Controller 中,那么您可以直接执行以下操作:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
guard let vm = (viewModel as? UICatalogVM)?.modelForRowAtIndexPath(indexPath) else {
return
}
let vc = ActivityIndicatorVC(viewModel: vm)
navigationController.pushViewController(vc, animated: true)
}
否则,您必须在导航 Controller 中添加当前的 View Controller ,然后才能将其添加到前面。
例如,如果您使用 Storyboard 并且当前 View Controller 是初始 View Controller ,那么您必须添加 UINavigationController 作为初始 View Controller ,然后将当前 View Controller 设置为该导航 Controller 的 rootViewController然后您可以使用
轻松地在didSelectRowAtIndexPath:
上推送新的 View Controller
navigationController.pushViewController(vc,动画:true)
关于ios - 如何从 UITableViewDelegate 的 didSelectRowtAtIndexPath 方法显示 UINavigationController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38428793/
当用户点击表格单元格时如何显示 UINavigationController?仅代码。 以下代码不起作用: func tableView(tableView: UITableView, didSele
我是一名优秀的程序员,十分优秀!