4

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.

Community
  • 1
  • 1
jaytrixz
  • 4,059
  • 7
  • 38
  • 57
  • Look this one: http://stackoverflow.com/questions/8162777/pass-object-from-vc-to-app-delegate/8163590#8163590 – Sudesh Kumar Dec 05 '11 at 10:10
  • Thanks but it just confused me more. All I need is to pass the value from the UITextField onto other VCs just like in Messages.app where you compose a text message on a ModalVC and the text value is passed to the cell and the message bubbles. – jaytrixz Dec 05 '11 at 10:27

3 Answers3

0

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.

Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
0

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";
GusOst
  • 4,105
  • 3
  • 19
  • 28
Musthafa P P
  • 645
  • 3
  • 7
  • 21
  • It's the same with the answer above but it still appears blank. – jaytrixz Dec 06 '11 at 02:46
  • I think u are not allocate memory for the String Variable. detailViewController.stringSecond=[NSStringalloc]init]; detailViewController.stringSecond=@"Some string"; – Musthafa P P Dec 06 '11 at 04:08
0

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);
user982270
  • 116
  • 2
  • 10
Tendulkar
  • 5,550
  • 2
  • 27
  • 53