使用 UITabBarControllerDelegate 方法:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
// I want to check the currently displayed view controller and the selected one to decide what to do
if (self.selectedViewController==self.viewController1){
if(viewController==self.viewController2 || viewController==self.viewController3){
// do stuff
}
}
基本上,我正在检查是否在 viewController1 可见时按下了不同的选项卡,做一些事情。根据Apple's documentation ,
@property(nonatomic, assign) UIViewController *selectedViewController
讨论
这个view controller是tab bar界面当前显示的自定义view。
现在奇怪的是: 在我的代码中,如您所见,我引用了 self.selectedViewController 来检查当前显示的那个,它始终返回与传递的 ViewController 参数完全相同的值,但返回的是 didSelect。也许 self.selectedViewController 已经设置为将作为 didSelect 的结果显示的那个?如果是这样,我应该如何或在何处访问它以获取在 didSelect 之前显示的内容?根据当前显示的正确路径->选择的新选项卡,我需要编写一些核心数据。
最佳答案
使用 tabBartController:shouldSelectViewController: 委托(delegate)方法。
关于ios - UITabViewController : how to access the correct visible tab bar (selectedViewController showing wrong results),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22190026/