【问题标题】:Conditional change between Child Views in PageViewPageView 中子视图之间的条件更改
【发布时间】:2019-09-22 06:09:28
【问题描述】:

我有一个 PageViewController,P,它包含两个子 ViewController,ABAB 都允许用户在表单中输入一些数据。如果用户开始编辑表单,我会在布尔变量中跟踪:

var formEdited = false;

如果用户想离开表单,并且 formEdited 为真,我想警告他们并说“您确定要放弃表单中的更改吗?”。如果他们确定,我想存储他们的数据。否则,我会让他们丢弃数据并继续刷卡。

因此,我尝试在 AB 中都这样做:

 override func viewWillDisappear(_ animated: Bool) {
    if (formEdited) {
        let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure you want to abandon the changes you have in the form?", preferredStyle: .alert);
        let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
            super.viewWillDisappear(animated);
        })
        let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
            // TODO:: what to do here
            self.myCustomFuctionToStoreData(); 
            super.viewWillAppear(true);

        }
        dialogMessage.addAction(ok);
        dialogMessage.addAction(cancel);
        self.present(dialogMessage, animated: true, completion: nil);
    }
}

因此,当我尝试从视图滑开时,我可以看到我的弹出窗口。如果我单击“取消”,则视图仍然存在。 (这就是我想要的)但是,如果我再次重试滑动,我将不再看到警告框,并且 UI 会发生变化。 (这不是我想要的。我希望它重新提示)

我相信我的代码需要在 viewWillDisappear 时做出更恰当的反应。我想我需要以某种方式防止视图在上面这行之后消失:

// TODO:: what to do here

注意:我尝试过其他一些帖子的答案,例如:How do I Disable the swipe gesture of UIPageViewController?Disable swipe gesture in UIPageViewController,甚至这两个:Disable UIPageViewController Swipe - SwiftChecking if a UIViewController is about to get Popped from a navigation stack?

最后两个可能最合适,但我不想禁用任何手势,也不知道如何注入预防措施。我只是想让从子视图中滑开成为一个条件函数。我将如何从 Swift 4 中的子视图( PageView 的子视图)执行此操作?

【问题讨论】:

    标签: ios swift user-interface


    【解决方案1】:

    事实证明,在 UIPageView 中实现条件滚动操作是微不足道的。这些是我为解决问题所采取的步骤。 (鼓励更新此代码以提高效率)

    首先,您的 UIPageViewController 不能是数据源。这意味着在子视图控制器中的滚动操作期间,不会注册任何内容。 (现在还可以)相反,您希望实现逻辑,以便将视图显示为子级可以调用的函数。这两个方法可以添加到你的 UIPageView 中:

    func goToNextView(currentViewController : UIViewController) {
        var movingIdx = 0;
        let viewControllerIndex = orderedViewControllers.index(of: currentViewController) ?? 0;
        if (viewControllerIndex + 1 <= (orderedViewControllers.count - 1)) {
            movingIdx = viewControllerIndex + 1;
        }
        self.setViewControllers([orderedViewControllers[movingIdx]], direction: .forward, animated: true, completion: nil);
    }
    
    func goToPreviousView(currentViewController : UIViewController) {
        var movingIdx = 0;
        let viewControllerIndex = orderedViewControllers.index(of: currentViewController) ?? -1;
        if (viewControllerIndex == -1) {
            movingIdx = 0;
        } else if (viewControllerIndex - 1 >= 0) {
            movingIdx = viewControllerIndex - 1;
        } else {
            movingIdx = orderedViewControllers.count - 1;
        }
        self.setViewControllers([orderedViewControllers[movingIdx]], direction: .reverse, animated: true, completion: nil);
    }
    

    注意事项:

    更新包含 ?? 的行是有意义的。 0;一种引发错误或显示一些默认屏幕的方法。

    orderedViewControllers 是这个 UIPageView 控制器包含的所有子视图的列表

    这些方法将从子视图中调用,因此将它们保留在这一层使它们非常可重用

    最后,在您的子视图中,您需要一种识别手势并对手势做出反应的方法:

    override func viewDidLoad() {
        super.viewDidLoad();
    
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
        swipeLeft.direction = .left
        self.view.addGestureRecognizer(swipeLeft)
    
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
        swipeRight.direction = .right
        self.view.addGestureRecognizer(swipeRight)
    
    }
    

    并处理手势:

    @objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
        if gesture.direction == UISwipeGestureRecognizerDirection.left {
            parentPageViewController.goToNextView(currentViewController: self);
        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.right {
            parentPageViewController.goToPreviousView(currentViewController: self);
        }
    }
    

    注意事项:

    在 handleGesture 函数中,您将添加条件检查以确定 goToNextView 或 goToPreviousView 是否正常。

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 2012-06-19
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      相关资源
      最近更新 更多