我在我的应用程序中有tableview,我想在tableview的单元格点击上推送一个新的视图控制器,但它不起作用。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let section = indexPath.section
let controller : ProductInfoViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ProductInfoViewController") as! ProductInfoViewController
controller.product = TableData[section]
self.navigationController?.pushViewController(controller, animated: true)
print("clicked section : \(section)")
// print(TableData[row])
}
有人知道我在这做什么错吗?我在故事板中有适当的视图控制器ID。控制台也没有错误。
答案 0 :(得分:1)
如果你的navigationController是nil那么你没有任何导航堆栈来推动viewcontroller on.It看起来你正在使用storyboard所以将这个控制器嵌入到navigationcontroller中。为此在storyboard中选择这个视图控制器点击顶部菜单中的“Editor”转到“嵌入”并选择navigationcontroller ...希望它有所帮助:)
答案 1 :(得分:0)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let section = indexPath.section
let controller : ProductInfoViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ProductInfoViewController") as! ProductInfoViewController
controller.product = TableData[section]
// execute in main thread
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.navigationController?.pushViewController(controller, animated: true)
}
print("clicked section : \(section)")
// print(TableData[row])
}
答案 2 :(得分:0)
为第二个视图控制器创建一个swift文件(SecondViewController.swift),并在相应的函数中输入:
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
答案 3 :(得分:0)
如果您的self.navigationController
即将出现,则意味着您的视图控制器未添加到导航堆栈中。因此您必须在didFinishLaunchingWithOptions中编写代码:方法
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.viewController = ViewController()
self.navigationController = UINavigationController(rootViewController: self.viewController)
self.window.rootViewController = self.navigationController!
self.window.makeKeyAndVisible()
self.navigationController.navigationBar.hidden = true
return true
}
试试这可能会对你有所帮助: -
答案 4 :(得分:0)
使用故事板名称并检查您将设置故事板ID,我的代码在下面,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("PlayViewController") as! PlayViewController
controller.titleLabel = (myArrayOfDict[indexpath.section].valueForKey("Title") as? String)!
self.navigationController?.pushViewController(controller, animated: true)
希望它有用