无法在iphone中将数据从一个视图传递到另一个视图

时间:2012-09-12 10:41:36

标签: iphone ios5 uiviewcontroller uiactionsheet

我在程序中包含了UIActionSheet。它包含2个按钮,例如A和B.当用户点击A按钮时,它会转到另一个视图。在该视图中,它包含3个文本字段。因此,在mainviewcontroller中,我将值分配给这些文本字段。我将粘贴下面的代码: -

在MainViewController.m中

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex 
  {
         AnotherViewController *A = [[AnotherViewController alloc] init];

          if (buttonIndex == 1) 
          {

              A.newtask = name;
              A.newsubtask = sub1;
              A.newduedate = dateName;
              A.newtime = timeName;


     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];                                                                    

     UpdateViewController   *viewController =[storyboard instantiateViewControllerWithIdentifier:@"update"];

     [self presentViewController:viewController animated:YES completion:NULL];


          }
   }

在AnotherViewcontroller.m中

 - (void)viewDidLoad
   {

              task_update.text = newtask;
              subtask_update.text =newsubtask;
              duedate_update.text = newduedate;
              time_update.text = newtime;

           NSLog(@"The task is :%@",newtask);
              [super viewDidLoad];
// Do any additional setup after loading the view.
    }

问题是我在newtask中获得null值。任何人都可以告诉我解决方案。 :(

1 个答案:

答案 0 :(得分:1)

在AppDelegate类中获取全局变量。喜欢 AppDelegate .h

NSString *newtask,*newsubtask,*newduedate,*newtime;

@property(nonatomic,retain)NSString *newtask,*newsubtask,*newduedate,*newtime;

AppDelegate中。米

@synthesize newtask,newsubtask,newduedate,newtime;

MainViewController.h

import“AppDelegate”

{
AppDelegate *appDelegate;
}

MainViewController.m

    (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex 
      {
appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
             AnotherViewController *A = [[AnotherViewController alloc] init];

              if (buttonIndex == 1) 
              {

                  appDelegate.newtask = name;
                  appDelegate.newsubtask = sub1;
                  appDelegate.newduedate = dateName;
                  appDelegate.newtime = timeName;


         UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];                                                                    

         UpdateViewController   *viewController =[storyboard instantiateViewControllerWithIdentifier:@"update"];

         [self presentViewController:viewController animated:YES completion:NULL];


              }
       }

AnotherViewcontroller.h

import“AppDelegate”

{
AppDelegate *appDelegate;
}

AnotherViewcontroller.m

- (void)viewDidLoad
   {
    appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

              task_update.text = appDelegate.newtask;
              subtask_update.text =appDelegate.newsubtask;
              duedate_update.text = appDelegate.newduedate;
              time_update.text = appDelegate.newtime;

           NSLog(@"The task is :%@",newtask);
              [super viewDidLoad];
// Do any additional setup after loading the view.
    }