How to send text from text field to another view controller

ⅰ亾dé卋堺 提交于 2019-12-23 12:52:00

问题


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.


回答1:


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.




回答2:


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);



回答3:


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";


来源:https://stackoverflow.com/questions/8383650/how-to-send-text-from-text-field-to-another-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!