2012-01-24 95 views
22

在故事板之前,我可以通過將插座拖放到課程來設置代表和數據源。通過故事板,我不能將插座拖到另一個視圖控制器;沒有目的地會迴應它。故事板 - 設置代表

如果我點擊一個視圖控制器對象,我可以在底部看到類的所有者,但只要我選擇包含插座的其他視圖控制器,舊的選擇不見了,所以我無法連接二。

這是蘋果的方式來說我們只應該以編程方式連接它們嗎?

回答

27

正確。在您的prepareForSegue:sender:方法中設置代表或其他數據。這裏有一個例子:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Check the segue identifier 
    if ([segue.identifier isEqualToString:@"showDetail"]) 
    { 
     // Get a reference to your custom view controller 
     CustomViewController *customViewController = segue.destinationViewController; 

     // Set your custom view controller's delegate 
     customViewController.delegate = self; 
    } 
} 
+3

如何設置UISplitViewController的委託?我看到的示例代碼通過獲取窗口的根視圖控制器來處理它,如下所示:'UISplitViewController * splitViewController =(UISplitViewController *)self.window.rootViewController;'雖然技術上是正確的,它的工作原理,它似乎是一個真正的與代理和視圖控制器設置在故事板之前的不錯方式相比,這是一種骯髒的方法... –

0

如果你的故事板賽格瑞目的地 - 視圖 - 控制器是一個UIViewController中那麼@Marco答案是正確的。但是,如果你的目標視圖控制器是一個UINavigationViewController,那麼你必須從UINavigationViewController獲得UIViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Check the segue identifier 
    if ([segue.identifier isEqualToString:@"chooseCategoryType"]) 
    { 
     // Get a reference of your custom view controller if your segue connection is an UIViewController 
     // CustomViewController *customViewController = segue.destinationViewController; 
     // Get a reference of your custom view controller from navigation view controller if your segue connection is an UINavigationViewController 
     CustomViewController *customViewController = [[[segue destinationViewController] viewControllers] objectAtIndex:0]; 

     // Set your custom view controller's delegate 
     customViewController.delegate = self; 
    } 
}