I have an iPhone app with a main view that has a UILabel and a UIButton. When the button is clicked, I display a second view. The second view has a text box and an OK button.
I want the user to enter something in the second view’s text box and then click the OK button; this should close the second view and apply the entered text to the UILabel back on the first view.
How can I expose the contents of the text box on the second view, and how do I access this from the first view? Also, how can I get the OK button on the second view to close the view (i.e. navigate back to the first view)?
Update: Since I’m coming from the .NET world, I can describe how I would do this same task in .NET, and that might make it clearer what I’m trying to do. In a .NET application, I would create a form (with the text box and button), and then display it using ShowDialog, which presents the form modally. I would add a public property to the form (called EnteredText or something) which returns whatever is in the text box. When my calling code continues from the ShowDialog call, I simply read the form’s EnteredText property and use it however, then dispose of the form.
I’m trying to do basically the same thing with an iPhone app.
Create a delegate protocol and assign the first view to be a delegate of the second view. Then add a method to the main view that conforms to the protocol.
Delegate class (myDelegate.h) would look something like this:
Then you add this to the second view header file:
And to your second views implementation:
Then your main view (header):
Then in your implementation for your mainview you’ll need to implement the method
Also when you open your second view from your main view you’ll need to set the delegate with:
Lastly, when your button is clicked call the delegate method (ie. talk to the main view):