0

I am trying to pass textfield text from one controller to another controller so I can email it. I am following an example, but I must be missing some little details. Basically the goal is to pass data textfield text from enteryournamecontroller controller to option controller's nsstring. The nsstring's string variable is being returned null.

EnterYourNameController.h

 @interface EnterYourNameController : UIViewController{

  UITextField *textField;

  id <EnterYourNameController> delegate;
 }
  @property (strong, nonatomic) NSString *stringEntered;
  @property (strong, nonatomic)  UITextField *textField;
  @property (strong, nonatomic) id delegate; 

EnterYourNameController.m

       -(void)button{
       stringEntered=textField.text;

      ((Options *) (self.delegate)).string = stringEntered;

           UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
Options *vc = [storyboard instantiateViewControllerWithIdentifier:@"Options"];
     [self.navigationController pushViewController:vc animated:YES];

       }

options.h

  @interface Options : UIViewController <MFMailComposeViewControllerDelegate> {

   NSString *string;
   }
   @property (strong,nonatomic) NSString* string;

   @end

option.m

     NSString *emailBody = [NSString stringWithFormat:@"%@",string];
Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
Two Face
  • 87
  • 7

2 Answers2

1

In -(void) button method, initialize the Options controller and set the value.

-(void)button{

       stringEntered=textField.text;
       Options *optViewController = [[Options alloc] init];
       optViewController.string = stringEntered;
       //call your optViewController here
  }
iOS
  • 3,526
  • 3
  • 37
  • 82
  • Still doesn't work for some odd reason. I made an NSLog to see if stringEntered was working correctly and it is working. – Two Face Aug 28 '13 at 12:23
  • Maybe its the way Im moving between controllers. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; Options *vc = [storyboard instantiateViewControllerWithIdentifier:@"Options"]; [self.navigationController pushViewController:vc animated:YES]; – Two Face Aug 28 '13 at 12:25
  • Try using [self.navigationController pushViewController:optViewController animated:YES]; in the last line of the method. – iOS Aug 28 '13 at 12:39
  • http://stackoverflow.com/questions/8429088/ios-storyboard-passing-data-navigationviewcontroller – iOS Aug 28 '13 at 12:40
  • http://stackoverflow.com/questions/14307316/how-can-i-pass-value-between-navigationcontroller-and-viewcontroller-with-storyb – iOS Aug 28 '13 at 12:40
  • The links may help you. – iOS Aug 28 '13 at 12:40
  • Ok, so its been the way I was pushing my controller. I knew I was missing something small. – Two Face Aug 28 '13 at 12:41
  • Yes. The actual data is been lost when you move between your controllers. – iOS Aug 28 '13 at 12:45
0

I hope, your string in option VC wouldn't live, when you switch between views. So, you must retain string property like this:

-(void)button
{
    Options *optViewController = [[Options alloc] init];
    optViewController.string = [[NSString stringWithString:textField.text] retain];
    //call your optViewController here
}
lykant
  • 516
  • 4
  • 17