Search code examples
objective-ciosuiviewcontrollercommunicationdelegation

Communication between controllers iOS


first of all I want to show you my storyboard:

enter image description here

I have a Label in the CUVCViewController (the first view of the left) and a UISlider in the CUVCViewControllerPageTwo (the second view). When I tap on the "page 2" button I can change the value of the UISlider. I want to show the value of the UISlider in the label, but I don't understand how I can do it! I have tried with delegation, but I don't understand how I can implement it!

Can someone help me? Thanks!!


Solution

  • Ok, so this one is pretty easy, but I'm really not sure how far you've got! I'm going to assume that you just want to know how to pass the data from the UISlider back to the first view controller.

    In the class of the first view controller, you need to implement the method

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    This is called just before the segue to the second view controller. you can then set your first view controller as a delegate to the second. So you would need to set up a @property on the second view controller of type 'first view controller' and call it, say, delegate (you could give it any name)

    In your -prepareForSegue: method, do this

    SecondViewController *secondController = segue.destinationViewController; secondController.delegate = self;

    Then from this point you can refer to your first view controller as delegate from your second. Prehaps you could pass the data back from the UISlider via a method call on the first, or by setting a property on the first

    eg [delegate sliderDidUpdate:sliderValue]