开发者

How can I return one or more values from a View?

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 ge开发者_运维百科t 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:

#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];


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜