0

I am running Xcode 7.3 and using Swift 2.2. I am having trouble passing data from my first view controller to the second view controller. I've created a variable string in my first view controller that holds the selected row in the tableview and it works. I have also created another variable string in my second view controller that should hold the name of the selected row, then pass the text to a label.

import UIKit
import Parse
import ParseUI

class FruitListPage: UIViewController, UITableViewDelegate, UITableViewDataSource {


var selectedFruit = String()

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

    let fruitName:Scanner2 = self.storyboard?.instantiateViewControllerWithIdentifier("scanPage") as! Scanner2
    fruitName.loadView()
    let nameShop = textArray[indexPath.row]
    print(nameShop)
    selectedFruit = nameShop

    print(selectedFruit)

    fruitName.selectedFruit2 = selectedFruit

}

Everything works completely fine unit here in the first view controller and I can see the fruit name every time I press a row in the tableview. The real problem is that selectedFruit2 string in the second view controller never grabs the data from the first view controller. Any clue?

import UIKit
import Parse

class Scanner2: UIViewController {

@IBOutlet var fruitNameLabel: UILabel!

var selectedFruit2 = String()

override func viewDidLoad() {
    super.viewDidLoad()

    print(selectedFruit2)

}

}
1
  • loadView documentation: "You should never call this method directly". Also note that you are setting selectedFruit2 after you have loaded the view. Removing the loadView will probably fix your problem although viewWillAppear(_:) will be probably safer than viewDidLoad Commented Mar 27, 2016 at 21:42

4 Answers 4

1

You could try making a global variable and in the override func prepareForSegue method, initialize it with tableView.indexPathForSelectedRow!.row. This is how I did it and it worked fine.

Sign up to request clarification or add additional context in comments.

Comments

0

Connect your viewController in storyBoard with a segue and set its identifier as "scanPageSegue" for instance... then:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("scanPageSegue", sender: textArray[indexPath.row])
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "scanPageSegue" {
        if let fruitName = segue.destinationViewController as? Scanner2, 
           selectedFruit = sender as? String {
            fruitName.selectedFruit2 = selectedFruit
        }
    }
}

2 Comments

Thanks it was very helpful and easy... I tried the prepareforsegue function before but the real important part for me was to connect storyboard with segue and to do performseguewithidentifier, then do prepareforsegue. I was stuck on this for nearly 3 days now. Is this something new in swift 2?
Not really, this is the basilar mechanism to use in storyBoard, it was the same also before swift :D Anyway... now you know it. Basically you never need to instantiate programmatically a VC with storyboard, use segue instead, you can also use a custom subclass for segue with your custom transitions.
0

Try with this, in your tableView:didSelectedRowAtIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let fruitName:Scanner2 = self.storyboard?.instantiateViewControllerWithIdentifier("scanPage") as! Scanner2
    selectedFruit = textArray[indexPath.row]
    fruitName.selectedFruit2 = selectedFruit
    self.presentViewController(fruitName,animated:true,completion:nil)
}

Comments

0

You could try this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let indexPath = self.tableView.indexPathForSelectedRow {
let destinationVC = segue.destinationController
destinationVC.string = textArray[indexPath] //this is the string in second view controller
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.