0

I've got a table view with X rows and i want that if I tap one of the rows it displays a view controller with a different value each. I know that i have to use prepareForSegue but i don't know how. I'll make an example: There's the table view with 4 rows. I tap the first row and it opens a view controller with a label that shows "1". I tap the second row and it opens a view controller with a label that shows "2". And so on. I don't want to create milions of view controllers, i'd like to have only one of them that changes every time.

You'll make me a big favor. Thanks

Campa
  • 55
  • 1
  • 7
  • 1
    Have you tried a TableView tutorial? Almost all of them cover the behavior that you are describing – Jose Llausas May 19 '16 at 18:44
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Jose Llausas May 19 '16 at 18:53
  • You might want to check out: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – Jose Llausas May 19 '16 at 18:53

2 Answers2

0

First you need to detect the user's selection.

This is done with the -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath function.

Once you have detected the selected row, you need to invoke the the next view controller. You can do this by instantiating the viewController you want to show using the storyboard: UIViewController* viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Your VC identifier here"];

To pass information to another controller you can create a property and set it like any other object:

[viewController setCustomArray:@[]]

Finally you need to present the view controller:

[self presentViewController:viewController animated:YES completion:nil]

Jose Llausas
  • 3,366
  • 1
  • 20
  • 24
0

You need to add the following in your tableViewController class

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

    text = indexPath.row
    performSegueWithIdentifier("YourSegue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier == "YourSegue" {
    let yourOtherVC = segue.destinationViewController as? SecondViewController
        yourOtherVC?.text = text
  }
}

In your SecondViewController class, add the following:

class SecondViewController: UITableViewController {

var text:String = ""
@IBOutlet weak var myLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    if text != nil {
        myLabel.text = text
    }
}

Hope this helps. :)

bhakti123
  • 833
  • 10
  • 22
  • Ok it works but not properly. It says _Use of uresolved identifier 'text'_ in the tableViewController. Do I need to create a 'text' var in the tableViewController or should I call it from the ViewController? – Campa May 20 '16 at 19:22
  • Yes you need to create a var in your `tableViewController` to store the selected row count. Then reference this var in your `prepareForSegue` to pass the data to the next viewController – bhakti123 May 21 '16 at 02:48