0

It shows that the source view controler does not have the string variable so what should I do?

This is the code in my source view controller

@IBOutlet var textfield: UITextField!
@IBOutlet var tofirstbutton: UIButton!
@IBOutlet var tosecondbutton: UIButton!

var s:String!

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "unwindtosecond"{
        self.s=self.textfield.text
    }

}

and this is the code in my destination view controller

@IBOutlet var textfield: UITextField!
@IBOutlet var tofirstbutton: UIButton!
@IBOutlet var tothirdbutton: UIButton!
@IBOutlet var label: UILabel!

@IBAction func unwindtosecond(Segue:UIStoryboardSegue){

    if Segue.identifier == "unwindtosecond" {
        var source:ViewController = Segue.sourceViewController as ViewController

        var s:String = source.s!
    }

I am getting an error saying that viewcontroller does not have a member named s.

  • This unwindtosecond() method is button action method or segue? – Amit89 May 23 '15 at 15:22
  • @Amit89 it is a segue I removed ibaction –  May 23 '15 at 15:52
  • You had it correct the first time. The function, `unwindtosecond` needs to be an IBAction or you won't be able to hook up the unwind segue in the storyboard. By source, and destination, you mean of the unwind segue? Which controller is the storyboard's initial controller (what is its class)? – rdelmar May 23 '15 at 16:32
  • Yes I corrected it. Yes I mean the source and destination of the unwind segue. The second one ( the lower one ) is the initial class. The upper one is the class I have reached using a modal segue. Now I want to go back from the upper one to the lower one by unwinding. –  May 24 '15 at 03:13

2 Answers2

0

Try these solutions:

  1. Protocol delegate: https://stackoverflow.com/a/24299231/1925852
  2. Setup a closure as a property: https://stackoverflow.com/a/24318588/1925852
Community
  • 1
  • 1
Thiêm
  • 155
  • 8
0

Here is some example code to implement a main controller segueing to a second VC, and the second unwinding to the main controller. Notice that in the function the second cVC unwinds to, you can refer to the second controller and access a variable in the second controller. If you want to initialize a variable in the second VC from the main VC, you can use a prepareForSegue in the main VC.

import UIKit

class ViewController: UIViewController {
var varInMainVC = ""

@IBAction func segueToSecondVCAction(sender: UIButton) {
    performSegueWithIdentifier("segueToSecondVC", sender: self)
}
@IBAction func unwindFromSecondVC(segue: UIStoryboardSegue) {
    // return here from second VC
    let vc = segue.sourceViewController as! SecondViewController
    varInMainVC = vc.varInSecondVC
    println("varInMainVC = \(varInMainVC)")

}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

import UIKit

class SecondViewController: UIViewController {
var varInSecondVC = ""
var varLoadedFromMainVC = 0

override func viewDidLoad() {
    super.viewDidLoad()
    varInSecondVC = "Test String to be returned"

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
Syed Tariq
  • 2,878
  • 3
  • 27
  • 37
  • The mistake I made was that I was using 'as viewcontroller' rather than as secondviewontroller thank you –  May 24 '15 at 03:38