简体   繁体   English

如何在两个UIViewcontrollers之间跳转

[英]How to jump between two UIViewcontrollers

I want to jump between two UIViewcontrollers 我想在两个UIViewcontrollers之间UIViewcontrollers

through swipe action. 通过滑动动作。

Currently, I implemented a UIScrollView , and added UIViewcontrollers (vc1 and vc2) on it, so 当前,我实现了UIScrollView ,并在其上添加了UIViewcontrollersUIViewcontrollersUIViewcontrollers ),因此

On vc1 在vc1上

swipe left : jump to vc2 向左轻扫:跳至vc2

swipe right : do nothing 向右滑动:不执行任何操作

On vc2 在vc2上

swipe left : do nothing 向左滑动:不执行任何操作

swipe right : jump to vc1 向右轻扫:跳至vc1

But what I want is: 但是我想要的是:

On vc1 在vc1上

swipe left : jump to vc2 向左轻扫:跳至vc2

swipe right : jump to vc2 向右轻扫:跳至vc2

On vc2 在vc2上

swipe left : jump to vc1 向左轻扫:跳至vc1

swipe right : jump to vc1 向右轻扫:跳至vc1

So, how to achieve? 那么,如何实现呢?

Try this 尝试这个

Steps: 脚步:

  1. In your view controller Create a UIPageViewController 在视图控制器中创建一个UIPageViewController

self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

  1. Add your both view controller on UIPageViewController UIPageViewController上添加两个视图控制器

NSArray *viewControllers = //add your two view controllers in array;

[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

  1. Set data source and Add UIPageViewController as child view controller 设置数据源并将UIPageViewController添加为子视图控制器

self.pageController.dataSource = self; [self addChildViewController:self.pageController];

Now, You need to implement two methods 现在,您需要实现两种方法

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    NSUInteger index = [(ViewController *)viewController index];

    if (index == 0) {
        return nil;
    }

    // Decrease the index by 1 to return
    index--;

    return viewControllers[index];//[self viewControllerAtIndex:index];//viewControllerAtIndex

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    NSUInteger index = [(ViewController *)viewController index];

    if (index == 1) {
        return nil;
    }
    index++;

    return viewControllers[index];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在UITabBarController中的两个UIViewControllers之间传递信息 - How to pass information between two UIViewControllers in a in a UITabBarController 两个UIViewController之间的UIPageViewController - UIPageViewController between two UIViewControllers 以编程方式在两个UIViewController之间切换 - Switch between two UIViewControllers programmatically 在两个UIViewControllers的视图之间滑动 - Swipe between two UIViewControllers' views 没有动画的两个UIViewController之间的过渡 - Transition between two UIViewControllers with no animation UIScrollview中的两个UIviewControllers-如何在它们之间调用方法 - Two UIviewControllers inside UIScrollview - how to call methods between them 在两个UIViewControllers之间进行编程转换(Xcode 9,Swift 4) - Programmatically transitioning between two UIViewControllers (Xcode 9, Swift 4) 两个人像UIViewController之间的横向景观 - Landscape Segue Between Two Portrait UIViewControllers Swift中一个屏幕上的两个UIViewController之间的交互 - Interaction between two UIViewControllers on one screen in Swift 单击信息按钮时如何在两个UIViewControllers之间进行翻转动画? - How to do the flip animation between two UIViewControllers while clicking info button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM