How to send text from text field to another view controller
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.
Comments
-
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 theUITextField
inComposeViewController
will be transferred to the table cell'sUILabel
in a custom cell inMasterViewController
and also to theDetailViewController
where anotherUILabel
will get the text value from theUITextField
. TheDetailViewController
is theViewController
loaded when the user taps the cells from theUITableViewCells
.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 over 12 yearsI 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 over 12 yearsI think it works because my testLabel turned blank. Any suggestions why it's like that?
-
jaytrixz over 12 yearsI'm sorry I didn't quite get it.
-
jaytrixz over 12 yearsIt's the same with the answer above but it still appears blank.
-
Musthafa P P over 12 yearsI think u are not allocate memory for the String Variable. detailViewController.stringSecond=[NSStringalloc]init]; detailViewController.stringSecond=@"Some string";
-
Kapil Choubisa over 12 yearsHave you mapped your testLabel from your interface builder by creating IBOutlet?
-
jaytrixz over 12 yearsThis is ok now. All I was looking for is NSUserDefaults. I just used it and it now works.