Reputation: 576
I have tableview in my application and I want to push a new view controller on cell click of tableview but it is not working.
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])
}
Does any one have idea what mistake I am doing here? I have proper view controller with ID in storyboard. No error is coming in console as well.
Upvotes: 0
Views: 1339
Reputation: 1158
If your navigationController is nil then you don't have any navigation stack to push viewcontroller on.It looks you are using storyboard so embed this controller to navigationcontroller.for this select this view controller in storyboard click "Editor" in top menu goto "Embed in" and select navigationcontroller...Hope it helps :)
Upvotes: 1
Reputation:
If your self.navigationController
is coming nil, means your view controller is not added to the navigation stack.. So you have to write code in didFinishLaunchingWithOptions: method
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
}
try this may be it will help you:-
Upvotes: 0
Reputation: 3245
Use the Storyboard Name and check you will set the Storyboard ID and my code is below,
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)
hope its helpful
Upvotes: 1
Reputation: 532
Create a swift file (SecondViewController.swift) for the second view controller and in the appropriate function type this:
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Upvotes: 0
Reputation: 1706
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])
}
Upvotes: 0