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

时间:2022-10-24 07:32:22

I want to get the current view controller at present in page view controller. how it can be done. Does it have some delegate to call or what.

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

3 个解决方案

#1


23  

I just had the same problem. Looks like the current controller is last in the list after you did change the page. This worked for me, but I don't know if it's always true.

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

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

#2


1  

Try get the first view controller in pageViewController.viewControllers

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

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

If you want to handle page changes, do it in a delegate method:

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

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

#3


0  

if you are in any UIView class (as UIButton, UITable or any other subclass) that is a subView of the UIViewController.view (or of any subView.subView.subView... of it)

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

you can check if there's a UIViewController in the superVew(s) chain, and stop when you find a UIViewController

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

something like this:

像这样的东西:

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

#1


23  

I just had the same problem. Looks like the current controller is last in the list after you did change the page. This worked for me, but I don't know if it's always true.

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

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

#2


1  

Try get the first view controller in pageViewController.viewControllers

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

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

If you want to handle page changes, do it in a delegate method:

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

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

#3


0  

if you are in any UIView class (as UIButton, UITable or any other subclass) that is a subView of the UIViewController.view (or of any subView.subView.subView... of it)

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

you can check if there's a UIViewController in the superVew(s) chain, and stop when you find a UIViewController

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

something like this:

像这样的东西:

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