Shawn Baek
Shawn Baek

Reputation: 1957

How to Push ViewController in UITableViewCell's @IBAction func

I have a UITableViewController and UITableViewCell and Cell's XIB Files.

My question is how to push UIViewController from cell?

in UITableViewCell.

 @IBAction func pushBtnTapped(sender: AnyObject) {
        // navigate to post view controller
        let guest = self.storyboard?.instantiateViewControllerWithIdentifier("GuestCollectionVC") as! GuestCollectionVC
        self.navigationController?.pushViewController(guest, animated: true)
}

I got an error message. self.storyboard is not UITableCell's member.

Upvotes: 1

Views: 930

Answers (1)

Echizzle
Echizzle

Reputation: 3459

Ideally you'd push from the table view controller, in the didSelectRowAtIndexPath method call. Why do you say you have to implement it in the cell? There are other ways you could do that but from what you're saying this might be the best shot...

self.performSegueWithIdentifier("YourSegueId", sender: nil)

Then in prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

//get the index path of the selected row
let indexPath = self.tableView.indexPathForSelectedRow

//just an example if you want to pass something to the other vc
let item = self.responseItems[indexPath!.row]

if segue.identifier == "YourSegueId" {

        YourVC = segue.destinationViewController as! YourVC

        YourVC.item = item
}
}

If you want to do it from the button you could do something like this in the prepareForSegue method... now you have the button and its index path

  let button = sender as! UIButton
  let view = button.superview
  let cell = view?.superview as! YourCell
  let indexPath = tableView.indexPathForCell(cell)

Upvotes: 1

Related Questions