2015-03-31 52 views
0

我有一個要求,我必須將一個字符串值從一個viewController傳遞到另一個按鈕單擊。我也需要使用tabBarController從當前viewController切換到secondViewController。我想:iOS - 將數據從一個viewController傳遞到另一個在xib中使用tabBarController的數據

FirstViewController.h

#進口 「SecondViewController.h」

@property(nonatomic, strong) SecondViewController *secondController; 

FirstViewController.m

-(IBAction)go:(id)sender 
{ 
    [email protected]"Demo Text"; 
    self.tabBarController.selectedIndex=1; //say secondViewController index is 1 
} 

SecondViewController.h

#import "FirstViewController.h" 

@property(nonatomic, strong) NSString *itemText; 

SecondViewController.m

-(void) viewWillAppear (BOOL) animated 
    { 
     NSLog(@"Comes from FirstViewController: %@",self.itemText); 
    } 

雖然切換的viewController,但它打印空白。代碼有什麼問題?

回答

1

我的猜測是你的第一個視圖控制器的「secondController」屬性不是在標籤欄控制器中指向的同一對象。

正確的做法是在標籤欄中從view controller array property訪問您的「SecondViewController」。

也就是說,這樣的事情:

-(IBAction)go:(id)sender 
{ 
    SecondViewController *secondVC = (SecondViewController *)[self.tabBarController.viewControllers objectAtIndex: 1]; 
    [email protected]"Demo Text"; 
    self.tabBarController.selectedIndex=1; 
} 
+0

應用的破碎工作的罰款。獲取錯誤: ' - [UINavigationController setitemText:]:無法識別的選擇器發送到實例' – Poles 2015-04-01 06:08:07

+1

我放下了我的代碼的第一個(和錯誤的)行,現在一切都應該是正確的。如果你在你的「go」行中設置了一個Xcode斷點,你期望的視圖控制器是「secondVC」嗎? – 2015-04-01 17:42:20

0

-(IBAction)go:(id)sender 
{ 
SecondViewController *secondVC = (SecondViewController *)[self.tabBarController.viewControllers objectAtIndex: 1]; 
    [email protected]"Demo Text"; 
    self.tabBarController.selectedIndex=1; 
} 
相關問題