如何 pause/resume/cancel iOS 中的事件

How to pause/resume/cancel an event in iOS

我有一个 UITabBarController,有时在 didSelectItem 委托中我需要暂停事件并显示一个弹出窗口。如果用户确认活动继续,否则活动将被取消。这是我的代码:

class YC_TabBarController: UITabBarController {

    var prevIndex: Int!
    var exitAction: (()->Bool)?

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        self.prevIndex = self.selectedIndex

        if self.prevIndex == 2 {

            guard self.exitAction != nil else {return}
            //pause
            let isExitAccepted: Bool = self.exitAction!()
            //if true -> resume
            //if false -> prevent from switching tab

        }

    }
}

我该怎么做?请帮忙

您应该确认 UITabBarControllerDelegate 在第一个视图控制器和 return false 是否在 shouldSelect viewController 中选择了所需的视图控制器。然后你应该显示你的弹出视图。在弹出视图 ok/confirm 按钮中,您可以更改 self.tabBarController

的选定视图控制器
class ViewController: UIViewController,UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarController?.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController is SecondViewController {
            //show alert
            return false
        } else {
            return true
        }
    }
    func popUpOkAction(_ sender:UIButton) {
        if let secVC = self.tabBarController?.viewControllers?.first(where: { [=10=] is SecondViewController }) {
            self.tabBarController?.selectedViewController = secVC
        }
    }
}

If you want to perform this from multiple view controllers rather than firstViewController you can confirm to UITabBarControllerDelegate in YC_TabBarController itself.