0

我正在尝试将文本字段文本从一个控制器传递到另一个控制器,以便我可以通过电子邮件发送它。我正在关注一个例子,但我必须遗漏一些小细节。基本上,目标是将数据文本字段文本从 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];

       }

选项.h

  @interface Options : UIViewController <MFMailComposeViewControllerDelegate> {

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

   @end

选项.m

     NSString *emailBody = [NSString stringWithFormat:@"%@",string];
4

2 回答 2

1

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

-(void)button{

       stringEntered=textField.text;
       Options *optViewController = [[Options alloc] init];
       optViewController.string = stringEntered;
       //call your optViewController here
  }
于 2013-08-28T12:19:05.917 回答
0

我希望,当您在视图之间切换时,选项 VC 中的字符串不会存在。因此,您必须像这样保留字符串属性:

-(void)button
{
    Options *optViewController = [[Options alloc] init];
    optViewController.string = [[NSString stringWithString:textField.text] retain];
    //call your optViewController here
}
于 2013-08-28T12:32:05.840 回答