我有一个应用程序,其中包含一个
UINavigationController
,它将UITabBarController
推入视图中。这个UITabBarController
有四个标签,其中一个显示一个自定义的UIViewController
实例,即EventInformationViewController
。在此自定义视图控制器中的按钮又会将另一个自定义视图控制器EventRatingAddViewController
推入视图中。该视图控制器中的操作应调用EventInformationViewController
实例中的方法。而以下代码却导致应用程序崩溃:// get the index of the visible VC on the stack
int myIindex = [self.navigationController.viewControllers indexOfObject:self.navigationController.visibleViewController];
// get a reference to the previous VC
EventInformationViewController *prevVC = (EventInformationViewController *)[self.navigationController.viewControllers objectAtIndex:myIindex - 1];
[prevVC performSelector:@selector(rateCurrentEvent:)];
我曾认为viewControllers属性保存了导航栈中所有VC的数组,因此当前可见VC的索引减一应该指向将当前可见VC推入视图的VC。然而,它似乎指向了我的UITabBarController
:
-[UITabBarController rateCurrentEvent:]: unrecognized selector sent to instance
这是怎么回事,更重要的是,我如何获得推送当前可见VC的VC的指针?
编辑:最终我创建了一个委托协议用于EventRatingAddViewController
,并将EventInformationViewController
分配为委托。这个方法很有效 - 尽管我认为应该有一种通过导航栈访问推送VC的方法。
.topViewController
而不是.visibleViewController
,因为后者可能是一个模态呈现的视图控制器,与你的导航控制器栈无关! - Douwe MaanmyIndex
和你获取的UITabBarController的索引吗? - Douwe Maan