Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push segue from cell click in table view to another view controller [duplicate]

I have one table view controller and by clicking the cell i am redirecting the user to detail view controller.But when i perform segue " present view controller" its working fine.But what i need is?

I need to perform push segue by clicking the table view cell.How to do that?

Here is my code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: BusinessDetailViewController = storyboard.instantiateViewControllerWithIdentifier("BusinessDetailViewController") as! BusinessDetailViewController
        vc.BusinessData = arrDict[indexPath.row]
        self.presentViewController(vc, animated: true, completion: nil) 
    }

I have tried this below line :

 self.navigationController?.pushViewController(vc, animated: true)

But it doesn't work.Any Solution Please !!

like image 670
user5513630 Avatar asked Dec 04 '22 00:12

user5513630


2 Answers

First of all your UITableViewController must have a UINavigationController

To do it rapidly embedded it to your table from the xCode menu

enter image description here:

Then , you must create the segue, ctrl-drag from the UITableViewController to the destination viewController:

enter image description here

Then select the segue and give it an identifier in the property inspector:

enter image description here

Now you are able to perform this segue in code:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    performSegueWithIdentifier("mySegue", sender: cell)
}
like image 186
Alessandro Ornano Avatar answered May 22 '23 09:05

Alessandro Ornano


If you are not using UINavigationController than add a segue in storyboard from table cell to destination viewController and give a identifier to segue.

then on cell click call

- performSegueWithIdentifier:sender:

and implement

- prepareForSegue:sender:
like image 25
jagdish Avatar answered May 22 '23 08:05

jagdish