2012-08-25 94 views
0

下面的代碼時:的iOS:警告處理didSelectViewController

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    if (viewController == [tabBarController.viewControllers objectAtIndex:0]) { 
     MySearchViewController *controller = viewController; 
     [[controller tableView] reloadData]; 
    } 
} 

產生以下警告:

Incompatible pointer types initializing 'MySearchViewController *__strong' with an expression of type 'UIViewController *__strong' 

什麼是投viewController到合適的類MySearchViewController調用它的方法的正確方法?

+0

你簡單地嘗試'MySearchViewController *控制器=(MySearchViewController *)的viewController;'? – DrummerB

+0

嘿,那太好了。目標C中是否正確鑄造? – Wells

+0

它只是告訴編譯器,你知道它是那種類型的,所以它不顯示警告。它不會對對象做任何事情。所以如果viewController變成另一種類型,它會崩潰。 – DrummerB

回答

2

就改成這樣:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    if (viewController == [tabBarController.viewControllers objectAtIndex:0] && [viewController isKindOfClass:[MySearchViewController class]]) { 
     MySearchViewController *controller = (MySearchViewController *)viewController; 
     [[controller tableView] reloadData]; 
    } 
} 
+0

好的遊戲,很好的努力。 – Wells