How to send text from text field to another view controller

16,252

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.

Share:
16,252
jaytrixz
Author by

jaytrixz

Filipino  fan

Updated on June 05, 2022

Comments

  • jaytrixz
    jaytrixz almost 2 years

    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.

  • jaytrixz
    jaytrixz over 12 years
    I edited my question above. Please see changes. So far, your answer is the closest one yet. Thanks in advance. I'll try your suggestions.
  • jaytrixz
    jaytrixz over 12 years
    I think it works because my testLabel turned blank. Any suggestions why it's like that?
  • jaytrixz
    jaytrixz over 12 years
    I'm sorry I didn't quite get it.
  • jaytrixz
    jaytrixz over 12 years
    It's the same with the answer above but it still appears blank.
  • Musthafa P P
    Musthafa P P over 12 years
    I think u are not allocate memory for the String Variable. detailViewController.stringSecond=[NSStringalloc]init]; detailViewController.stringSecond=@"Some string";
  • Kapil Choubisa
    Kapil Choubisa over 12 years
    Have you mapped your testLabel from your interface builder by creating IBOutlet?
  • jaytrixz
    jaytrixz over 12 years
    This is ok now. All I was looking for is NSUserDefaults. I just used it and it now works.