Search code examples
iosuiviewcontrollerswift2uitableviewrowaction

Unable to switch to another UIViewController when “Edit” UITableViewRowAction is pressed


I want my swift app to open an UIViewController (called "SecondViewController") when the UITableViewRowAction "Edit" is pressed. However, my code below doesn't do anything; when I press the "Edit" action, absolutely nothing happens.

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

//Edit action
let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Edit") { (editAction, indexPath) -> Void in

    let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController
    self.navigationController?.pushViewController(secondViewController, animated: true)

}

return [editAction]

}

How could this be solved? Is this even possible? If more information is needed, please comment.


Solution

  • I think the navigationController is nil. You can add the navigationController either through the storyboard or create it programatically and make the ViewController containing tableView as the rootViewController. This should work