-2

I am new to iOS development. one thing i want to ask when i click on the tableViewCell i want to move to new viewController but when i clicked it is giving me noting.

Here is my code-

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

    let row = indexPath.row
    if row == 4
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyboard.instantiateViewControllerWithIdentifier("contact") as! contactsandlists
        navigationController?.pushViewController(secondViewController, animated: true)
        print("error")
    }

Please resolve my problem

vaibhav
  • 4,038
  • 1
  • 21
  • 51
Bansal
  • 67
  • 1
  • 1
  • 6

4 Answers4

0

Try to use this presentation.

This will present you're controller but it will be without navigation bar.

 self.presentViewController(secondViewController, animated: true, completion: nil)

If you would like to present with navigation bar.

 self.navigationController.pushViewController(secondViewController, animated: true)
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
0

Do you have a navigationController? If you don't, nothing is going to happen and you print "error". Another way is to change the root view controller of the window property that you find in AppDelegate

    let appDelegate = UIApplication.sharedApplication().delegate

    if let window = appDelegate?.window {
        window?.rootViewController = secondViewController
        window?.makeKeyAndVisible()
    }

Another approach is to use create a segue between a tableviewcell and the other view controller and use performSegueWithIdentifier.

performSegueWithIdentifier("segueId", sender: nil)
robertsan
  • 1,591
  • 3
  • 14
  • 26
  • can u explain perform segue indentifier method – Bansal Sep 16 '16 at 10:59
  • In order to use performSegueWithIdentifier you have to build a segue. Go in the interface builder, drag a segue between the table cell and the new view controller you want to show when a user taps the cell. Select the segue, go to the identity tab and fill the identifier tab with some text like "detailViewSegue". Go in the primary view controller and execute performSegueWithIdentifier("detailViewSegue", sender: nil). Then implement the method prepareForSegue. You can read more here: http://stackoverflow.com/questions/24040692/prepare-for-segue-in-swift . You will differentiate this way. – robertsan Sep 16 '16 at 11:06
0

Make NavigationController to your initial view.

  • Select secondViewController view
  • Go To Editor--> Embed In --> Navigation Controller.
  • set the identifier of that NavigationController using identity inspector inside storyboard see image below.

enter image description here

vaibhav
  • 4,038
  • 1
  • 21
  • 51
0

1.You have to embed your ViewController which contain your tableview in storyboard by this procedure

GoTo Storyboard -> Click on Your Controller -> Editor (In Top Menu ) -> Embed IN -> Navigation Controller

  1. Write down this code to push your Controller

    let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("contact") as! contactsandlists
    self.navigationController.pushViewController(secondViewController, animated: true)
    
Sumit Jangra
  • 651
  • 5
  • 16