I am making an app using a utility application template. I am trying to access the value of a UITextField from the FlipSideVewController class.
In the MainViewController.h file I have –
@interface MainViewController : UIViewController <UISplitViewControllerDelegate>{
UITextField *textField;
NSString *myText;
}
@property (retain, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic, retain) NSString *myText;
-(IBAction)pressButton:(id)sender;
In the MainViewController.m file –
myText = [[NSString alloc] initWithFormat: textField.text];
NSLog(@"%@",myText);
I am creating the FlipSideViewController in the MainViewController class using the following code –
FlipsideViewController *controller = [[[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil] autorelease];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
This prints the value of the textfield in the console without any problems. The problem happens when I try to access the value of the textfield in the FlipSideVewController class (after the user presses the go button).
In the FlipViewController class I have –
MainViewController *obj = [[MainViewController alloc] init ];
NSString *abc = obj.textField.text;
NSLog(@"%@",abc);
The FlipSideVewController nib file is loaded fine without any problems. However the console output is (null) when in FlipSideVewController.
I will appreciate any help.
Thanks
You should go to your MainViewController and declare your textField as a property first and synthesize it, so you can access it using obj.textField. And if you have just created obj using alloc and init, you wont have any text in the textField instance Variable.
MainViewController.h
MainViewController.m
and you could use
Now this should do it and you can access this textField by obj.textField in your other class. But you still wont get its value if you are initializing it in your other class because you will be creating a brand new obj whose textField.text will be blank( unless you have overrided its designated initializer to set the textField.text value).
Declare NSString *abc as instance variable
and then as property
After you create your FlipSideViewController,
Remove the code where you create obj.
This will do it.