first time asking after learning many useful things here!
I have a VC1 with a button and label. The button is coded to present VC2 programmatically (without segue in IB). VC2 has a tableview with the cells containing string values.
When I click on the cell in VC2, I am trying to get the string value of the selected cell and pass it back to the label.text in VC1.
First ViewController code:
class VC1: UIViewController {
... ...
@IBOutlet weak var LabelText: UILabel!
var passedString = "Example"
override func viewWillAppear(animated: Bool) {
LabelText.text = "\(passedString)"
}
@IBAction func chooseLabelTextBtnPressed(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("VC2") as! VC2
self.presentViewController(vc, animated: true, completion: nil)
}
SecondViewController code:
class VC2: UIViewController, UITableViewDelegate, UITableViewDataSource {
... ...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow;
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! VC2_tableViewCell!;
let valueToPass = currentCell.IBOutletLbl.text
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("VC1") as! VC1
viewController.passedString = valueToPass!
//self.presentViewController(viewController, animated: true , completion: nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
I hoped func viewWillAppear() in VC1 would update the String value of the label when VC2 is dismissed, but it doesn't.
I cannot use presentViewController from VC2 to VC1, because it might open again the VC1 instead of going back, and then other variables in VC1 would be inaccessible.
Help me! Thanks!