2013-01-10 34 views
0

您好,我添加了一個準備segue代碼以傳輸信息,但我收到一個錯誤。從視圖控制器轉換到標籤欄控制器與導航控制器在每個選項卡錯誤

我有一個標籤欄控制器。它有4個選項卡。每個選項卡都有一個導航控制器,以及一個VC作爲根視圖控制器。

從標籤1 - >導航控制器 - > VC 1我需要的值,以TAB2 - >導航控制器---> VC1

(也就是在連接到導航控制器的SEGUE選項卡,或者在標籤2根視圖) 預先感謝您

錯誤:customizableViewControllers]:無法識別的選擇發送到實例0xf19bd60 (但我要標籤2(索引1))? 我的錯誤在哪裏?

if ([segue.identifier isEqualToString:@"ResultsSegue"]) 

     { 
//the root VC at tab 2 
      ResultsIndexViewController*controller=[[ResultsIndexViewController alloc]init]; 

      UITabBarController *tabController = [segue destinationViewController]; 
      controller = (ResultsIndexViewController*)[[tabController customizableViewControllers]objectAtIndex:1];//crashing here 
      controller.resultsArray=[NSMutableArray arrayWithArray:_matchedResultsArray]; 

     } 
    } 
+0

另外,我需要這方面的代表嗎? –

+0

你的錯誤基本上意味着,'tabbarcontroller'不是'UITabBarController'。可以肯定,'[segue destinationViewController]'返回一個'UITabBarController'?那是,如何配置賽格? – SAE

+0

該segue去一個導航控制器,它應該鏈接到tabbar控制器? –

回答

1

代碼

[1] UITabBarController *tabController = [segue destinationViewController]; 
[2] controller = (ResultsIndexViewController*)[[tabController customizableViewControllers]objectAtIndex:1];//crashing here 

這部分是完全的誤解。

在[1]中,您將一個destinationViewController(類型UIViewController)分配給UITabBarController類型的對象。這是編譯器合法的(UITabBarController繼承自UIViewController,所以它們都是UIViewController類型),但它並不真實反映發生了什麼。您的延期將是UIViewController或它的UINavigationController

所以在[2]當你試圖使用你的tabController作爲標籤欄控制器,它崩潰(因爲它不是一個)。

您需要重新設計您的項目......忘記Segue並使用標籤欄控制器。像...

- (IBAction)moveToTab2:(id)sender { 
    ResultsViewController* resultsVC = (ResultsViewController*) 
       [[self.tabBarController viewControllers] objectAtIndex:1]; 
    resultsVC.resultsArray= 
       [NSMutableArray arrayWithArray:_matchedResultsArray]; 
    [self.tabBarController setSelectedViewController:resultsVC; 
} 
+0

好吧,雖然重要的是將數據分配給新的VC –

+0

一種可行的方法是在你的應用程序委託中聲明一個屬性,首先從你的VC中設置它,然後在你的第二個VC中獲取它。這對你有用嗎? (不是最好的,因爲它反應全局,但它確實允許你分離你的vcs ...) – foundry

+0

我想,extern會工作(不是首選)我想我需要使用@協議,但我有問題抓住 –

相關問題