Pass value from second ViewController back

19 views
Skip to first unread message

Bjorn Chin Fo Sieeuw

unread,
Jan 16, 2015, 5:16:03 AM1/16/15
to swift-l...@googlegroups.com
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?

Bjorn Chin Fo Sieeuw

unread,
Jan 16, 2015, 8:37:40 AM1/16/15
to swift-l...@googlegroups.com
Solved!

Op vrijdag 16 januari 2015 11:16:03 UTC+1 schreef Bjorn Chin Fo Sieeuw:
Reply all
Reply to author
Forward
0 new messages