【问题标题】:Completion block isn't called when updating UIPageViewController during rotation在旋转期间更新 UIPageViewController 时不调用完成块
【发布时间】:2013-10-09 09:26:07
【问题描述】:

我正在尝试在轮换期间更新UIPageViewController 中的当前页面,如下所示:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSArray *controllers = @[someViewController];
    UIPageViewControllerNavigationDirection direction = UIPageViewControllerNavigationDirectionForward;

    [self.pageController setViewControllers:controllers direction:direction animated:YES completion:^(BOOL finished) {
        /// This completion block isn't called ever in this case.
    }];
}

但是,由于某种原因,在这种情况下永远不会调用完成块,而当我从其他地方调用具有相同参数的相同方法时,它会按预期调用。

谁能确认这种奇怪的行为,如果这是UIPageViewController 中的一个错误,还是只是对我的滥用?

更新

我发现如果我将参数animated 更改为NO 将按预期调用完成块!所以,在我看来,这是UIPageViewController 的一个错误。

【问题讨论】:

  • 在我的情况下,当我尝试设置我在 self.pageViewController.viewControllers.firstObject 中已有的相同控制器时遇到了问题

标签: ios objective-c uipageviewcontroller screen-rotation


【解决方案1】:

遇到同样的问题,发现以下情况:如果someViewController 是当前可见控制器或pageController.viewControllers?.first 并且animated 标志设置为true,则调用setViewControllers 而不调用完成处理程序。最简单的方法是调用setViewControllers 并将animated 标志设置为false。不会显示任何动画,但无论如何都会调用完成处理程序。但在大多数情况下,您需要显示动画。因此,这里唯一可能的解决方法是检查要设置的 viewController 是否不是可见的。如果是,您应该调用 setViewControllers 并将 animated 标志设置为 false:

let completionBlock: (Bool) -> Void
let isAnimated = (newController != pageController.viewControllers?.first)

pageController.setViewControllers([newController],
                                  direction: .forward,
                                  animated: isAnimated, 
                                  completion: completionBlock)

希望对你有所帮助!

【讨论】:

    【解决方案2】:

    试一试:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [self newMethod];
      // or  [self performSelector:@selector(newMethod) withObject:self afterDelay:0.01];
    }
    
    - (void)newMethod{
        NSArray *controllers = @[someViewController];
        UIPageViewControllerNavigationDirection direction = UIPageViewControllerNavigationDirectionForward;
    
        [self.pageController setViewControllers:controllers direction:direction animated:YES          completion:^(BOOL finished) {
            ///
        }];
    }
    

    【讨论】:

    • 这里有什么区别?
    • 因为您在委托方法中执行此操作。如果你这样做。它将从 saparat 方法中调用。如果这也不起作用。尝试使用延迟 0.01 的 perforselector。
    • 将代码移动到新方法并调用该方法并不意味着它将在另一个线程中执行。不过,我只想知道这是UIPageViewController 的错误还是对我的滥用。
    • 你试过了吗?好吧,按照你的想法去做
    猜你喜欢
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多