1) 确保 startFetchingFromServer 在您 selectedViewControllers 的头文件中声明(objOpponentInfoSettingsViewController.h 和 objOpponentInfoBuddiesViewController.h)
2)将您的 SelectedViewController 转换为适当的类。它是 UIViewController,因此您的编译器正在 UIViewController.h 中寻找 startFetchingFromServer 方法声明,而您希望它查看您的自定义类。
if(opponentInfoTabBarController.selectedViewController == objOpponentInfoSettingsViewController)
{
(objOpponentInfoSettingsViewController *)[opponentInfoTabBarController.selectedViewController startFetchingFromServer];
}
else if(opponentInfoTabBarController.selectedViewController == objOpponentInfoBuddiesViewController)
{
(objOpponentInfoBuddiesViewController *)[opponentInfoTabBarController.selectedViewController startFetchingFromServer];
}
希望这可以帮助!
编辑:更好的是检查 selectedViewController 是否属于特定类:
if(opponentInfoTabBarController.selectedViewController isKindOfClass: [OpponentInfoSettingsViewController class])
{
(OpponentInfoSettingsViewController *)[opponentInfoTabBarController.selectedViewController startFetchingFromServer];
}
else if(opponentInfoTabBarController.selectedViewController isKindOfClass: [OpponentInfoBuddiesViewController class])
{
(OpponentInfoBuddiesViewController *)[opponentInfoTabBarController.selectedViewController startFetchingFromServer];
}
这样,您不必维护对实例的全局引用,只需调用“startFetchingFromServer”方法即可。
通过在调用它之前添加对“startFetchingFromServer”选择器的检查,您可以使这段代码更加健壮。
就像是:
if ([theViewController respondsToSelector:@selector(startFetchingFromServer)]){
[theViewController startFetchingFromServer];
}