【问题标题】:Not able pass the data from one view to another in iphone无法在 iphone 中将数据从一个视图传递到另一个视图
【发布时间】:2012-09-12 10:41:36
【问题描述】:

我在我的程序中包含了 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.
    }

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

【问题讨论】:

    标签: iphone ios5 uiviewcontroller uiactionsheet


    【解决方案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

    导入“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];
        
        
                  }
           }
    

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

    【讨论】:

    • :不,我仍然在 newtask 中得到空值.. :(
    • 在另一个视图控制器中查看更新代码 appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];添加此行视图确实加载了
    • :非常感谢..我明白了.. :)
    猜你喜欢
    • 2013-03-02
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    相关资源
    最近更新 更多