如何在页面视图控制器中获取当前视图控制器

时间:2012-12-14 11:21:17

标签: iphone objective-c ios ipad uiviewcontroller

我想在页面视图控制器中获取当前的视图控制器。怎么做。是否有一些代表要打电话或是什么。

3 个答案:

答案 0 :(得分:23)

我遇到了同样的问题。在您更改页面后,当前控制器看起来像列表中的最后一个。这对我有用,但我不知道它是否总是如此。

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    UIViewController *vc = [pageViewController.viewControllers lastObject];
}

答案 1 :(得分:1)

尝试在 pageViewController.viewControllers中获取第一个视图控制器

    if let vc = pageViewController.viewControllers?[0] {
        ... vc is current controller...
    }

如果要处理页面更改,请在委托方法中执行此操作:

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if let vc = pageViewController.viewControllers?[0] {
        ... vc is current controller...
    }
}

答案 2 :(得分:0)

如果您在任何UIView类(如UIButton,UITable或任何其他子类)中,它是UIViewController.view(或其任何subView.subView.subView ...)的子视图

你可以检查superVew(s)链中是否有UIViewController,并在找到UIViewController时停止

类似的东西:

UIViewController* controllerFound = nil;
for (UIView* next = [self superview]; next; next = next.superview) {
    UIResponder* nextResponder = [next nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        controllerFound = (UIViewController*)nextResponder;
    }
}