How to send text from a text field to another view controller

I am creating an application that behaves like the default Message.app on the iPhone, where the user composes a text message in a UITextField and, by clicking the Submit button, the UITextField in ComposeViewController will be transferred to the UILabel table UILabel in the user cell in MasterViewController , as well as in DetailViewController , where another UILabel will get a text value from a UITextField . DetailViewController is a ViewController loaded when a user selects cells from UITableViewCells .

I do read related articles below, but this does not work on my end.

  • How to send text from a text field to another class?
  • How to see text from one text box in another text box?
  • How to send text field value to another class

Could you advise me how to implement this correctly? I know this easily. I just don’t know why it doesn’t work. ComposeVC is the ModalViewController , and DetailVC is the view that loads when the user deletes a cell in the MasterVC 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 a UILabel inside my DetailVC.

+4
source share
4 answers

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

in view 2 .h file

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

in the .m file @synthesize str;

Now you can send your text from view1 to view 2, for example:

 objView2.str = txtField.text; 

To view one text field in another use secondTextField.text = firstTextField.text;

Hope this helps me tell if you are looking for something else.

0
source

Try the following:

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

In the first view, you need to create a link as follows:

 #import "SecondView.h" SecondView *detailViewController = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil]; detailViewController.stringSecond = @"Some string"; 
0
source

take one variable in the application delegate, for example, in appdelegate.h

 @interface appdelegate { NSString *str1; } @property (nonatomic, retain) NSString *str; 

Synthesize it in a .m file.

set the text after editing the text field (app del.str = textfield.text ie setting value). And use the same where you want.

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

I think this is what you are looking for:

Transferring data between view managers

hope this helps, please let me know if you have more problems.

-1
source

Source: https://habr.com/ru/post/1384670/


All Articles