简体   繁体   English

如何从视图返回一个或多个值?

[英]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 . 我有一个iPhone应用程序,其主视图具有UILabelUIButton When the button is clicked, I display a second view. 单击该按钮后,我将显示另一个视图。 The second view has a text box and an OK button. 第二个视图具有一个文本框和一个OK按钮。

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. 这将关闭第二个视图,并将输入的文本重新应用到第一个视图的UILabel上。

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 (ie 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. 更新:由于我来自.NET领域,因此我可以描述如何在.NET中执行相同的任务,这可能使我想做的事情更加清楚。 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. 在.NET应用程序中,我将创建一个表单(带有文本框和按钮),然后使用ShowDialog进行显示,该对话框以模态形式显示该表单。 I would add a public property to the form (called EnteredText or something) which returns whatever is in the text box. 我将在表单中添加一个公共属性(称为EnteredText ),该属性返回文本框中的内容。 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. 当我的调用代码从ShowDialog调用继续时,我只需读取表单的EnteredText属性并使用它,然后处置该表单。

I'm trying to do basically the same thing with an iPhone app. 我正在尝试使用iPhone应用程序执行基本相同的操作。

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: 委托类(myDelegate.h)如下所示:

#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. 我是Android的新手,但是从我看过的所有教程中,您似乎都需要使用Intent将信息从第二个视图传递回第一个视图。 As I said I'm new to Android so there might be a different/better way to do it. 如我所说,我是Android的新手,所以可能会有其他/更好的方式来做到这一点。

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. 如我所见,您对iOS上的常规编码样式不太熟悉,建议您开始阅读Apple文档,其中涵盖了许多在启动iOS项目时必须了解的基本知识。

The iOS Application Programming Guide is a great starting point. iOS应用程序编程指南是一个很好的起点。 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. 这似乎与显示值或响应原始事件无关,后者是视图的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM