简体   繁体   中英

Open new UIViewController when clicking on UITableViewCell?

I have a simple table view that when I click on the cell, I want it to open a new UIViewController. Currently I have it so that it goes to a particular View Controller now, but it seems as if every cell leads to the same View Controller. I want each one to go to a different one. Here is my attempt:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var indexPath = self.tableView.indexPathForSelectedRow()
    let navController = self.storyboard?.instantiateViewControllerWithIdentifier("ChatController") as UINavigationController

    navController.hidesBottomBarWhenPushed = true
    let chatController = navController.viewControllers.first as ChatController
    chatController.currentUserName = self.firstName + " " + self.lastName
    chatController.chatWith = chatNames[indexPath!.row]
    chatController.currentUserObjectID = self.objectID
}

However, this doesn't seem to work as I get an error saying Storyboard doesn't contain a view controller with identifier 'ChatController'' . If it helps, the segue first goes through a navigation controller then to the desired view controller.

In the UITableViewDelegate method (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath call [self performSegueWithIdentifier:kSegueIdentifier sender:self];

And, according to the indexPath you can set a different value that will be passed to your next view controller through the method prepareForSegue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM