为什么我的数据不通过视图控制器传递?

时间:2013-08-28 12:07:35

标签: ios objective-c string

我正在尝试将文本字段文本从一个控制器传递到另一个控制器,以便我可以通过电子邮件发送它。我正在效仿一个例子,但我必须遗漏一些细节。基本上,目标是将数据文本域文本从enteryournamecontroller控制器传递到选项控制器的nsstring。 nsstring的字符串变量返回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];

2 个答案:

答案 0 :(得分:1)

-(void) button方法中,初始化Options控制器并设置值。

-(void)button{

       stringEntered=textField.text;
       Options *optViewController = [[Options alloc] init];
       optViewController.string = stringEntered;
       //call your optViewController here
  }

答案 1 :(得分:0)

我希望,当您在视图之间切换时,选项VC中的字符串将无法生效。所以,你必须保留这样的字符串属性:

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