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)
}
}
loadViewdocumentation: "You should never call this method directly". Also note that you are settingselectedFruit2after you have loaded the view. Removing theloadViewwill probably fix your problem althoughviewWillAppear(_:)will be probably safer thanviewDidLoad