【发布时间】:2012-12-02 09:30:37
【问题描述】:
我想在页面视图控制器中获取当前视图控制器。如何做到这一点。它是否有一些委托要调用或什么。
【问题讨论】:
标签: iphone objective-c ios ipad uiviewcontroller
我想在页面视图控制器中获取当前视图控制器。如何做到这一点。它是否有一些委托要调用或什么。
【问题讨论】:
标签: iphone objective-c ios ipad uiviewcontroller
我也遇到了同样的问题。在您更改页面后,当前控制器似乎是列表中的最后一个。这对我有用,但我不知道它是否总是正确的。
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
UIViewController *vc = [pageViewController.viewControllers lastObject];
}
【讨论】:
尝试在 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...
}
}
【讨论】:
如果您在任何 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;
}
}
【讨论】: