编程iOS按钮在第一次点击时执行操作,然后在第二次点击时执行

问题描述 投票:0回答:2

我正在构建第三方iOS应用程序。作为我的主视图控制器的一部分,我给用户提供按下按钮直接链接到iOS键盘设置的选项,以便他们可以启用我的键盘。当他们返回应用程序时,它会将他们带到下一个视图,在那里他们可以在空文本字段上测试键盘。

当用户点击应用程序上的启用键盘按钮时,我调用IBAction将它们链接到设置应用程序,如下所示:

@IBAction func enableKeyboard(_ sender: Any) {
        let settingsUrl = URL(string: "\(UIApplicationOpenSettingsURLString)")!
        UIApplication.shared.open(settingsUrl)
    }

但是,我同时对我的textViewController有一个segue,这样当用户从设置返回应用程序时,他们会立即转到textViewController。

enter image description here

我如何编程我的按钮,以便第一次点击它们进行设置,它们返回到主视图控制器,然后第二次点击将它们带到下一个视图?

swift segue uistoryboardsegue ibaction
2个回答
1
投票

翻转按钮的选定状态,然后在决定要执行的操作时对其进行测试。

@IBAction func touchUpInside(button: UIButton) {
    if !button.isSelected {
        button.isSelected = true
        // Now open settings
    } else {
       // Perform segue
    }
}

按钮类用于跟踪自己的状态,因此我认为这比在视图控制器中跟踪点击仅需要2个状态的变量更可取。


0
投票

您可以向控制器添加一些状态。它不仅适用于2个状态('打开设置'和'打开下一个视图'),您将能够管理更多状态。

您可以在控制器中添加状态变量:

enum ControllerState {
    case needSettings, needNext
}
private var state: ControllerState = .needSettings

@IBAction func yourButtonHandler(sender: UIButton) {
    switch state {
    case .needSettings:
        state = .needNext
        // Open settings
    case .needNext:
        // Open next view controller
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.