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

发布于 2024-12-19 16:23:47 字数 1510 浏览 1 评论 0原文

我正在制作一个应用程序,其行为类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦魇绽荼蘼 2024-12-26 16:23:47

您可以简单地在另一个 viewController 上创建属性。假设您的文本字段位于 view1 上,并且您想将其发送到 view2 上,则:

在视图 2 中
.h 文件

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

.m 文件中的
@合成str;

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

objView2.str = txtField.text;

用于在另一种用途中查看一个文本字段的文本
第二个文本字段.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

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

in .m file
@synthesize str;

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

objView2.str = txtField.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.

风苍溪 2024-12-26 16:23:47

在应用程序委托中采用一个变量,如 appdelegate.h

@interface appdelegate
{
NSString *str1;
}

@property (nonatomic, retain) NSString *str;

在 .m 文件中合成它。

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

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

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

@interface appdelegate
{
NSString *str1;
}

@property (nonatomic, retain) NSString *str;

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.

NSString *str = appdel.str(getting value);
子栖 2024-12-26 16:23:47

试试这个:

#interface SecondView:UIViewController 
{
    NSString *stringSecond;
}

@property(nonatomic, retain) NSString *str;

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

#import "SecondView.h"

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

detailViewController.stringSecond = @"Some string";

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文