转到推动可见视图控制器的视图控制器

时间:2010-07-06 12:28:26

标签: iphone uinavigationcontroller

我的应用有一个UINavigationController,可以将UITabBarController推入视图中。此UITabBarController有四个标签,其中一个显示自定义UIViewControllerEventInformationViewController的实例。此自定义视图控制器中的按钮又将另一个自定义视图控制器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的数组,因此当前可见的减去1的索引应该指向将当前可见VC推入视图的VC。相反,它似乎指向我的UITabBarController

-[UITabBarController rateCurrentEvent:]: unrecognized selector sent to instance

该怎么做,更重要的是,如何获得一个指向VC的指针,将VC推向当前可见的VC?

编辑:我最终为EventRatingAddViewController创建了一个委托协议,并将EventInformationViewController指定为委托。这很好用 - 我仍然认为应该有办法通过导航堆栈访问推送VC。

1 个答案:

答案 0 :(得分:6)

我很确定那个UITabBarController确实推送了你当前的视图控制器,但是你要找的是这个UITabBarController选项卡之一的视图控制器,视图控制器当UITabBarController将视图控制器推到导航堆栈上时,UITabBarController中可见。可能这个UITabBarController将视图控制器推到堆栈上,因为可见选项卡的视图控制器告诉它这样做,所以它会是这样的:[self.tabBarController.navigationController pushViewController:someViewController];

在查看控制器被推入堆栈时查找UITabBarController中显示的视图控制器的方法是使用.selectedViewController属性,这样会产生类似这样:

// get the index of the visible VC on the stack
int currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];
// get a reference to the previous VC
UITabBarController *prevVC = (UITabBarController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex - 1];
// get the VC shown by the previous VC
EventInformationViewController *prevShownVC = (EventInformationViewController *)prevVC.selectedViewController;
[prevShownVC performSelector:@selector(rateCurrentEvent:)];