4

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 !!

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
user5513630
  • 1,709
  • 8
  • 24
  • 48
  • I din't perform any drag and drop and i din't set any identifier name.I need to perform fully programatically.I refered hat also.But it din't help me – user5513630 May 03 '16 at 11:39
  • Is your current view controller embedded in a navigation controller? – Paulw11 May 03 '16 at 11:45

4 Answers4

12

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)
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
4

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:
Rohit Khandelwal
  • 1,778
  • 15
  • 23
jagdish
  • 166
  • 5
  • 1
    Thanks @jagdish this worked for me! I dragged from the Cell, selected in the outline view, to the View Controller, gave the segue an Identifier and then passed it in to performSegue(). Note that I'm not doing anything yet with the data in my example as its a Work in Progress to learn Swift. But the segue is working: `func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let row = indexPath.row let section = indexPath.section performSegue(withIdentifier: “MySegueID”, sender: self) }` – Lucy Oct 21 '17 at 14:54
0

Without a navigation controller it's not possible to push a controller. Your self.navigationController property is nil (you can check it)

How to solve it?

A :You need to embed your view controller in navigation controller either via code or via storyboard

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • in code.How to call my current view controoler with navigation controller.Beacuse there are more viewcontrollers.So if i do it now via storyboard.Sure my all views will get affect – user5513630 May 03 '16 at 11:50
0

try this code, its working for me,

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){

            let storyboard = UIStoryboard(name: "Main", bundle: nil)

            let controller = storyboard.instantiateViewControllerWithIdentifier("PlayViewController") as! PlayViewController

            self.navigationController?.pushViewController(controller, animated: true)
        }

and set the storyboard Id, example given below,

enter image description here

hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30