xcode 如何将文本从文本字段发送到另一个视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8383650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:40:39  来源:igfitidea点击:

How to send text from text field to another view controller

iphoneiosxcodeuitextfielduilabel

提问by jaytrixz

I'm making an app that behaves something like the default Messages.app in iPhone where a user composes a text message in a UITextFieldand upon tapping the Send button, the value of the UITextFieldin ComposeViewControllerwill be transferred to the table cell's UILabelin a custom cell in MasterViewControllerand also to the DetailViewControllerwhere another UILabelwill get the text value from the UITextField. The DetailViewControlleris the ViewControllerloaded when the user taps the cells from the UITableViewCells.

我正在制作一个应用程序,其行为类似于 iPhone 中的默认 Messages.app,其中用户在 a 中撰写文本消息,UITextField点击发送按钮后,UITextFieldin的值ComposeViewController将传输到UILabel自定义单元格中的表格单元格中MasterViewController以及DetailViewController另一个UILabel将从UITextField. 的DetailViewControllerViewController,当用户从轻敲细胞加载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 ModalViewControllerwhile DetailVC is the view that loads when the user taps the cell in the MasterVC's table.

你能指导我如何正确实施吗?我知道这很容易。我只是不知道为什么它不起作用。ComposeVC 是一个ModalViewController而 DetailVC 是当用户点击 MasterVC 表中的单元格时加载的视图。

Thanks a lot!

非常感谢!

Below is my code for ComposeVC.h:

下面是我的 ComposeVC.h 代码:

    UITextField *messageTextField;

    @property (nonatomic, retain) IBOutlet UITextField *messageTextField;

    - (IBAction)buttonPressed:(id)sender;

for ComposeVC.m

对于 ComposeVC.m

    synthesize messageTextField;

    -(IBAction)buttonPressed:(id)sender
    {
        DetailVC *detailVC = [[DetailVC alloc] init];
        detailVC.messageText = messageTextField.text;
    }

for DetailVC.h

对于 DetailVC.h

    NSString *messageText;

    @property (nonatomic, copy) NSString *messageText;

for DetailVC.m

对于 DetailVC.m

    @synthesize messageText;

    - (void)viewLoad
    {
        testLabel.text = messageText;
    }

testLabel is the UILabel inside my DetailVC.

testLabel 是我的 DetailVC 中的 UILabel。

采纳答案by Kapil Choubisa

You can simply create property on another viewController. Suppose your textField is on view1 and you want to send it on view2 then:

您可以简单地在另一个 viewController 上创建属性。假设您的 textField 在 vi​​ew1 上,并且您想在 view2 上发送它,然后:

in view 2 .h file

在视图 2 .h 文件中

#interface view2:UIViewController {
NSString *str;
}
@property (nonatomic, retain) NSString *str;

in .m file @synthesize str;

在 .m 文件中@synthesize str;

Now you can send your text from view1 to view 2 like:

现在,您可以将文本从视图 1 发送到视图 2,例如:

objView2.str = txtField.text;

For viewing one textfield's text in another use secondTextField.text = firstTextField.text;

要查看另一个文本字段的文本,请使用 secondTextField.text = firstTextField.text;

Hope this helped let me know if you are looking for something different.

希望这有助于让我知道您是否正在寻找不同的东西。

回答by Musthafa P P

Try this one:

试试这个:

#interface SecondView:UIViewController 
{
    NSString *stringSecond;
}

@property(nonatomic, retain) NSString *str;

In First View u have to create an reference like this:

在第一个视图中,您必须创建一个像这样的引用:

#import "SecondView.h"

SecondView *detailViewController = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];

detailViewController.stringSecond = @"Some string";

回答by Tendulkar

take one variable in the app delegate like in appdelegate.h

在 app 委托中采用一个变量,如 appdelegate.h

@interface appdelegate
{
NSString *str1;
}

@property (nonatomic, retain) NSString *str;

In .m file synthesize it.

在 .m 文件中合成它。

set the text after editing the textfield(app del.str=textfield.text i.e setting value).And use the same wherever you want.

编辑文本字段后设置文本(app del.str=textfield.text 即设置值)。并在任何地方使用相同的内容。

NSString *str = appdel.str(getting value);