如何将文本从文本字段发送到另一个视图控制器 [英] How to send text from text field to another view controller
问题描述
我正在制作一个行为类似iPhone中默认Messages.app的应用程序,其中用户在 UITextField
中编写文本消息,然后点击发送"按钮,即 ComposeViewController
中的> UITextField 将被转移到 MasterViewController
中的自定义单元格中表单元格的 UILabel
中,也将被传输到中DetailViewController
,其中另一个 UILabel
将从 UITextField
获取文本值. DetailViewController
是用户从 UITableViewCells
轻触单元格时加载的 ViewController
.
I'm making an app that behaves something like the default Messages.app in iPhone where a user composes a text message in a UITextField
and upon tapping the Send button, the value of the UITextField
in ComposeViewController
will be transferred to the table cell's UILabel
in a custom cell in MasterViewController
and also to the DetailViewController
where another UILabel
will get the text value from the UITextField
. The DetailViewController
is the ViewController
loaded when the user taps the cells from the UITableViewCells
.
我实际上阅读了下面的相关文章,但对我而言没有用.
I actually read related articles below but it doesn't work on my end.
- 如何将文本从文本字段发送到另一个类?
- 如何在另一个文本字段中查看一个文本字段中的文本?
- 如何将文本字段值发送给另一个类
您能指导我如何正确实施吗?我知道这很容易.我只是不知道为什么它不起作用.ComposeVC是 ModalViewController
,而DetailVC是当用户点击MasterVC表中的单元格时加载的视图.
Can you please guide me on how to properly implement this? I know it's easy. I just don't know why it's not working. ComposeVC is a ModalViewController
while DetailVC is the view that loads when the user taps the cell in the MasterVC's table.
非常感谢!
下面是我的ComposeVC.h代码:
Below is my code for ComposeVC.h:
UITextField *messageTextField;
@property (nonatomic, retain) IBOutlet UITextField *messageTextField;
- (IBAction)buttonPressed:(id)sender;
对于ComposeVC.m
for ComposeVC.m
synthesize messageTextField;
-(IBAction)buttonPressed:(id)sender
{
DetailVC *detailVC = [[DetailVC alloc] init];
detailVC.messageText = messageTextField.text;
}
对于DetailVC.h
for DetailVC.h
NSString *messageText;
@property (nonatomic, copy) NSString *messageText;
对于DetailVC.m
for DetailVC.m
@synthesize messageText;
- (void)viewLoad
{
testLabel.text = messageText;
}
testLabel是我的DetailVC中的UILabel.
testLabel is the UILabel inside my DetailVC.
推荐答案
您可以简单地在另一个viewController上创建属性.假设您的textField在view1上,并且您想在view2上发送它,那么:
You can simply create property on another viewController. Suppose your textField is on view1 and you want to send it on view2 then:
在视图2中.h文件
#interface view2:UIViewController {
NSString *str;
}
@property (nonatomic, retain) NSString *str;
.m文件中的
@synthesize str;
in .m file @synthesize str;
现在,您可以将文本从view1发送到view 2,例如:
Now you can send your text from view1 to view 2 like:
objView2.str = txtField.text;
用于在另一个用途中查看一个文本字段的文本secondTextField.text = firstTextField.text;
For viewing one textfield's text in another use secondTextField.text = firstTextField.text;
希望这可以让我知道您是否正在寻找不同的东西.
Hope this helped let me know if you are looking for something different.
这篇关于如何将文本从文本字段发送到另一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!