2012-09-12 190 views
1

我在程序中包含了UIActionSheet。它包含2個按鈕,分別表示A和B.當用戶單擊A按鈕時,它會轉到另一個視圖。在該視圖中,它包含3個文本字段。因此,在mainviewcontroller中,我將這些值分配給這些文本字段。我將粘貼下面的代碼: -無法將數據從一個視圖傳遞到另一個視圖iphone

在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越來越空值。任何人都可以告訴我解決方案。 :(

回答

1

以全局變量在AppDelegate類。像 的AppDelegate .H

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

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

AppDelegate的內容M

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


       } 
     } 

AnotherViewcontroller.h

進口 「的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. 
    } 
+0

:沒有靜止,我在newtask越來越空值.. :( – Div

+0

請參閱另一個視圖控制器中的更新代碼appDelegate =(AppDele門*)[[UIApplication sharedApplication]委託];添加此行視圖確實加載 – iSanjay

+0

:非常感謝你..我明白了.. :) – Div

相關問題