如何将文本从文本字段发送到另一个视图控制器
我正在制作一个应用程序,其行为类似于 iPhone 中的默认 Messages.app,其中用户在 UITextField
中撰写短信,并在点击“发送”按钮后,UITextField< 的值
ComposeViewController
中的 /code> 将被传输到 MasterViewController
中自定义单元格中表格单元格的 UILabel
以及 DetailViewController
代码>其中另一个UILabel
将从UITextField
获取文本值。 DetailViewController
是当用户点击 UITableViewCells
中的单元格时加载的 ViewController
。
我实际上阅读了下面的相关文章,但它对我来说不起作用。
- 如何将文本从文本字段发送到另一个类?
- 如何在另一个文本字段中查看一个文本字段中的文本?
- 如何将文本字段值发送到另一个类
您能指导我如何正确实现这一点吗?我知道这很容易。我只是不知道为什么它不起作用。 ComposeVC 是一个 ModalViewController,而 DetailVC 是当用户点击 MasterVC 表格中的单元格时加载的视图。
多谢!
下面是我的 ComposeVC.h 代码:
UITextField *messageTextField;
@property (nonatomic, retain) IBOutlet UITextField *messageTextField;
- (IBAction)buttonPressed:(id)sender;
对于 ComposeVC.m
synthesize messageTextField;
-(IBAction)buttonPressed:(id)sender
{
DetailVC *detailVC = [[DetailVC alloc] init];
detailVC.messageText = messageTextField.text;
}
对于 DetailVC.h
NSString *messageText;
@property (nonatomic, copy) NSString *messageText;
对于 DetailVC.m
@synthesize messageText;
- (void)viewLoad
{
testLabel.text = messageText;
}
testLabel 是我的 DetailVC 中的 UILabel。
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.
- How to send the text from textfield to another class?
- How to see text from one text field in another text field?
- How to send text field value to another class
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.
Thanks a lot!
Below is my code for ComposeVC.h:
UITextField *messageTextField;
@property (nonatomic, retain) IBOutlet UITextField *messageTextField;
- (IBAction)buttonPressed:(id)sender;
for ComposeVC.m
synthesize messageTextField;
-(IBAction)buttonPressed:(id)sender
{
DetailVC *detailVC = [[DetailVC alloc] init];
detailVC.messageText = messageTextField.text;
}
for DetailVC.h
NSString *messageText;
@property (nonatomic, copy) NSString *messageText;
for DetailVC.m
@synthesize messageText;
- (void)viewLoad
{
testLabel.text = messageText;
}
testLabel is the UILabel inside my DetailVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简单地在另一个 viewController 上创建属性。假设您的文本字段位于 view1 上,并且您想将其发送到 view2 上,则:
在视图 2 中
.h 文件
.m 文件中的
@合成str;
现在,您可以将文本从视图 1 发送到视图 2,例如:
用于在另一种用途中查看一个文本字段的文本
第二个文本字段.text = 第一个文本字段.text;
希望这有助于让我知道您是否正在寻找不同的东西。
You can simply create property on another viewController. Suppose your textField is on view1 and you want to send it on view2 then:
in view 2
.h file
in .m file
@synthesize str;
Now you can send your text from view1 to view 2 like:
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.
在应用程序委托中采用一个变量,如 appdelegate.h
在 .m 文件中合成它。
编辑文本字段后设置文本(app del.str=textfield.text 即设置值)。并在您想要的任何地方使用相同的内容。
take one variable in the app delegate like in appdelegate.h
In .m file synthesize it.
set the text after editing the textfield(app del.str=textfield.text i.e setting value).And use the same wherever you want.
试试这个:
在第一个视图中,您必须创建一个像这样的引用:
Try this one:
In First View u have to create an reference like this: