Pass value from second ViewController back

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Pass value from second ViewController back

Bjorn Chin Fo Sieeuw
I am trying to pass a boolean value from myViewController2 back to ViewController, but it seems that the segue is not working. The page I am looking at is: http://stackoverflow.com/questions/26089152/sending-data-with-segue-with-swift

The code I have:

ViewController:

class ViewController: UIViewController, SCNSceneRendererDelegate, ChangeUserValueDelegate {

    

    var currentUserValue:Bool = false


  override func viewDidAppear(animated: Bool) {

    super.viewDidAppear(animated)

    println(currentUserValue)


  }


    func changeUser(toValue:Bool) {

        currentUserValue = toValue

        

    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "mySegue"{

            var yourNextViewController = (segue.destinationViewController as myViewController2)

            yourNextViewController.delegate = self

            println("hello")

        }

    }

}


myViewController2:

protocol ChangeUserValueDelegate {

    func changeUser(toValue:Bool)

}


class myViewController2: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

    

    var delegate:ChangeUserValueDelegate?


    @IBAction func saveColor(sender: UIBarButtonItem) {

        delegate?.changeUser(true)

        self.navigationController?.popViewControllerAnimated(true)

    }

}


When ViewController appears 'false' is printed even after clicking the saveColor button on myViewController2. On clicking saveColor, I need to return to ViewController and print 'true'. How do I get the segue working?

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/0572e241-6206-4b96-8ea5-0559a00bd4ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Pass value from second ViewController back

Bjorn Chin Fo Sieeuw
Solved!

Op vrijdag 16 januari 2015 11:16:03 UTC+1 schreef Bjorn Chin Fo Sieeuw:
I am trying to pass a boolean value from myViewController2 back to ViewController, but it seems that the segue is not working. The page I am looking at is: <a href="http://stackoverflow.com/questions/26089152/sending-data-with-segue-with-swift" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fquestions%2F26089152%2Fsending-data-with-segue-with-swift\46sa\75D\46sntz\0751\46usg\75AFQjCNE0cIQ0HHvZ7UI9vGwEZoDAcz-ERQ';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fquestions%2F26089152%2Fsending-data-with-segue-with-swift\46sa\75D\46sntz\0751\46usg\75AFQjCNE0cIQ0HHvZ7UI9vGwEZoDAcz-ERQ';return true;">http://stackoverflow.com/questions/26089152/sending-data-with-segue-with-swift

The code I have:

ViewController:

class ViewController: UIViewController, SCNSceneRendererDelegate, ChangeUserValueDelegate {

    

    var currentUserValue:Bool = false


  override func viewDidAppear(animated: Bool) {

    super.viewDidAppear(animated)

    println(currentUserValue)


  }


    func changeUser(toValue:Bool) {

        currentUserValue = toValue

        

    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "mySegue"{

            var yourNextViewController = (segue.destinationViewController as myViewController2)

            yourNextViewController.delegate = self

            println("hello")

        }

    }

}


myViewController2:

protocol ChangeUserValueDelegate {

    func changeUser(toValue:Bool)

}


class myViewController2: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

    

    var delegate:ChangeUserValueDelegate?


    @IBAction func saveColor(sender: UIBarButtonItem) {

        delegate?.changeUser(true)

        self.navigationController?.popViewControllerAnimated(true)

    }

}


When ViewController appears 'false' is printed even after clicking the saveColor button on myViewController2. On clicking saveColor, I need to return to ViewController and print 'true'. How do I get the segue working?

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/2eaedd49-fff2-49e8-b866-44e9b3094537%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.