1

我在我的程序中包含了 UIActionSheet。它包含 2 个按钮,比如 A 和 B。当用户单击 A 按钮时,它会转到另一个视图。在该视图中,它包含 3 个文本字段。所以在主视图控制器中,我将值分配给这些文本字段。我将粘贴下面的代码:-

在 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];


          }
   }

而在另一个视图控制器.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.
    }

问题是我在新任务中得到了空值。谁能告诉我解决方案。:(

4

1 回答 1

1

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

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

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

应用委托。米

@synthesize newtask,newsubtask,newduedate,newtime;

主视图控制器.h

导入“AppDelegate”

{
AppDelegate *appDelegate;
}

主视图控制器.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];
    
    
              }
       }

另一个视图控制器.h

导入“AppDelegate”

{
AppDelegate *appDelegate;
}

另一个视图控制器.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.
    }
于 2012-09-12T10:56:50.170 回答