2012-07-25 40 views
0

我需要在兩個視圖控制器之間傳遞數據。這裏是我的代碼。ViewControllers之間不傳遞數據

對於第一視圖控制器

-(void)editBotton { 

    Carttable *second=[[Carttable alloc]init]; 

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:second]; 
    nav.modalTransitionStyle=UIModalTransitionStyleCrossDissolve; 

    [self.navigationController pushViewController:second animated:YES]; 

    NSString *temp=titlela.text;//titlela is UILabel 

    NSLog(@"%@",temp); 
    self.cart=second; 
    cart.cnftitle=temp; 
} 
在我cartable視圖或者Controller.h

@property(nonatomic,retain)NSString *cnftitle; 

和我合成太

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSLog(@"%@",cnftitle); 
} 

一個的NSLog打印我的文本在標籤其中另一個打印作爲NULL ....

我錯過了什麼?

+0

視圖被加載並且在第一次推視圖控制器送到堆棧值是正確的打印。之後您正在設置該值。問題就在你的NSLog所在的位置。如果您在將視圖控制器推入堆棧之前設置字符串值,它應該可以工作。 – Dima 2012-07-25 19:24:38

回答

4

您在視圖加載後分配值。

移動pushViewController:之前,你的任務(因爲這是在視圖加載點)

1

檢查這個代碼,

-(void)editBotton { 

    Carttable *second=[[Carttable alloc]init]; 
    [second.view setAlpha:1.0f]; 

    second.cnftitle = titlea.text; 

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:second]; 
    nav.modalTransitionStyle=UIModalTransitionStyleCrossDissolve; 

    [self.navigationController pushViewController:second animated:YES]; 
} 

希望這會爲你工作。

享受編碼:)

1
****in xode 4.5.1** storyboard** 
     we assume wee want to passed data between 
    viewcontroller1 to viewcontroller2 
    in the viewcontroller1 in .h class we import viewcontroller2.h 
    then we declare variable in viewcontroller2 ex: Nsstring *data; 
    then we can passed data like this.. 
    in the viewcontroller1 we can code like this(in button event) 

     ViewController3 *vc3=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"]; 

     vc3.data=textfield1.text; 
     vc3.image=imageview.image; 
     [self presentViewController:vc3 animated:YES completion:nil]; 

note=set the storyboard id of viewcontroller2 as ViewController2 
     it is on the right hand side utility menu 3rd tab 
(select viewcontroller2 and set storyboard id) 
相關問題