tags:

views:

109

answers:

4

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.

A: 

I'm new to Android, however from all the tutorials I've gone through it seems like you need to use an Intent to pass information from your second view back to your first. As I said I'm new to Android so there might be a different/better way to do it.

Brian Driscoll
Thanks, but it's iPhone here.
MusiGenesis
Disregard my answer if this really is meant for iPhone... though my guess would be that iPhone uses a similar mechanism.
Brian Driscoll
c'mon why the downvote? This was re-tagged Android when I posted my answer.
Brian Driscoll
Was the down-vote really necessary? This Android answer was based on the OP specifically mentioning the Android platform originally.
Joshua Nozzi
I think all the platforms support multiple ways of doing this - a method on the view, an object passed in and returned, etc. I'd be happy with just one way for the iPhone.
MusiGenesis
+1 to counteract the down-vote. Not your fault.
MusiGenesis
+6  A: 

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:

#import <Foundation/Foundation.h>
@protocol myDelegate
- (void) textContent:(NSString *)text;
@end

Then you add this to the second view header file:

#import "myDelegate.h"
@interface secondView : UIView {

id <myDelegate> delegate;
}
@property (nonatomic,retain) id <myDelegate> delegate;

And to your second views implementation:

@synthesize delegate;

Then your main view (header):

#import "myDelegate.h"

@interface mainView: UIView <myDelegate> {

}

Then in your implementation for your mainview you'll need to implement the method

- (void) textContent:(NSString *)text {
//Do something
}

Also when you open your second view from your main view you'll need to set the delegate with:

[secondView setDelegate:self];

Lastly, when your button is clicked call the delegate method (ie. talk to the main view):

[delegate textContent:string];
Ben
That sounds great, but I don't know how to do any of these things. Can you include some code or a link to a good sample?
MusiGenesis
How do I get the second view to "close"? It has a Back button up at the top, but I want to use the OK button I've added.
MusiGenesis
This line: `[secondView setDelegate:self];` is causing my app to crash and disappear. Any idea why this might be happening? I have the code in a try/catch block, but the app is crashing anyway.
MusiGenesis
Do you have the synthesize line in your second view?
Ben
Never mind - I forgot the `@synthesize delegate;` line in my second view.
MusiGenesis
I'm using a navigation controller, like so: `[self.navigationController pushViewController:aTextInputViewController animated:YES];`. Is there a removeViewController method?
MusiGenesis
Thanks for the tips. This approach is the reverse of what I'm trying to do (since it's the second view calling something in the first rather than vice versa), but it works fine and it's gotten me started.
MusiGenesis
Yes you can do a popViewController like [self.navigationController popViewController];
Ben
You should get the bounty "tip" in a week. Thanks again for all your help.
MusiGenesis
Thanks! I appreciate it.
Ben
A: 

As I see you're not very familiar with the general coding styles on iOS, I recommend to start reading the Apple documentation which covers a lot of basic stuff which you have to know when starting an iOS project.

The iOS Application Programming Guide is a great starting point. Be sure that you will learn more when reading the documentation first.

brutella
Thank you for the link. I can't agree with your documentation-first prescription, though. I've read much of the documentation, but I usually don't get very much out of it unless I'm actively digging into coding at the same time.
MusiGenesis
sure, there are some issues which has to be tried out in the first hand, but I can't recommend the documentation enough - they are full with great examples and guidelines.
brutella
A: 

This sounds like it should be in the controller, which should be the delegate of the text field (so as to receive changes to the text) and the target of the button (so as to receive the action message to tell it to do whatever it's supposed to do with the text). This doesn't seem to have anything to do with displaying values or responding to raw events, which are the jobs of a view.

Peter Hosey