不关闭视图的UIAlertController操作

时间:2017-05-22 08:33:31

标签: ios swift uialertcontroller

我创建了一个带有文本字段的UIAlertController,以及一个将验证码发送到用户电话号码的操作。但是,该操作也会解除警报控制器,使用户无法输入验证码。有没有办法让警报动作不解除警报控制器?

   let alertController = UIAlertController(title: "Verification", message: "a code has been sent to your phone number", preferredStyle: .alert)

let confirm = UIAlertAction(title: "Confirm", style: .default, handler: {
    alert -> Void in
    //verifies code
})

let resend = UIAlertAction(title: "Resend", style: .default, handler: {
        alert -> Void in
        //resends code
    })

let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
    (action : UIAlertAction!) -> Void in

})

alertController.addTextField { (textField : UITextField!) -> Void in
    textField.placeholder = "Enter code"
}

alertController.addAction(confirm)
alertController.addAction(resend)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)

3 个答案:

答案 0 :(得分:0)

您可以使用UITextFieldDelegateisEnabledUIAlertAction属性来达到预期效果。

<强>步骤:

  1. 您的班级应该向UITextFieldDelegate
  2. 确认
  3. 您的提醒操作应声明为类级属性
  4. 在显示警报
  5. 之前禁用警报操作
  6. 配置文本字段并将委托设置为您的类
  7. 实施shouldChangeCharactersIn:方法
  8. 如果文本长度大于所需长度,则启用警报操作,否则禁用警报操作
  9. <强>代码:

    // Your Class should confirm to UITextFieldDelegate
    class ViewController: UIViewController, UITextFieldDelegate
    {
        // Verify alert action, alert button that want to be disabled
        var verifyAction : UIAlertAction!
    
        // Shows the alert
        @IBAction func showAlert(sender : UIButton)
        {
            let alert    = UIAlertController(title: "Verify", message: "Please Enter the verification code to continue", preferredStyle: UIAlertControllerStyle.alert)
            verifyAction = UIAlertAction(title: "Verify", style: UIAlertActionStyle.default) { (action) in
                // Do verification and other stuffs
            }
    
            // Configures textfield properties and delegate
            alert.addTextField { (textField) in
                textField.placeholder = "Verification Code"
                textField.delegate    = self
            }
    
            // Disables the button
            verifyAction.isEnabled = false
            alert.addAction(verifyAction)
            self.present(alert, animated: true, completion: nil)
        }
    
        // Textfield delegate method for observing the text change
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
        {
            // if text length is greater than 0 enables the button, else disables it
            let existingText = textField.text as NSString?
            if let replacedText = existingText?.replacingCharacters(in: range, with: string), replacedText.characters.count > 0
            {
                verifyAction.isEnabled = true
            }
            else
            {
                verifyAction.isEnabled = false
            }
            return true
        }
    }
    

    注意: 我在发布代码之前写了这个答案,所以它不是基于你在问题中提供的代码。这是一个通用的答案,如果你想让我这么做,我会编辑答案。

答案 1 :(得分:0)

通过使用默认警报控制器,点击任何警报操作按钮将关闭警报控制器。您可能必须创建自己的自定义视图/视图控制器并执行相应的操作或使用某些第三方库。

答案 2 :(得分:0)

试试此代码,因为这对我来说很好用:

func createAlert() {
    // initializing
    let alertControler = UIAlertController(title: "Your Title",
                                           message: "Your message",
                                           preferredStyle: .Alert)
    // create actions
    let confirmAction = UIAlertAction(title: "Confirm", style: .Default, handler: {
        (action) -> Void in self.confirmAction()
    })

    let resendAction = UIAlertAction(title: "Resend", style: .Default, handler: {
        (action) -> Void in self.resendAction()
    })

    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: {
        (action) -> Void in self.dismissAlert()
    })

    // add actions
    alertControler.addAction(confirmAction)
    alertControler.addAction(resendAction)
    alertControler.addAction(cancelAction)

    self.presentViewController(alertControler, animated: true, completion: nil)
}

// initiate this func to your class
@objc private func dismissAlert() {
    self.dismissViewControllerAnimated(true, completion: nil)
}

@objc private func confirmAction() {
    // yuor action here
}

@objc private func resendAction() {
    // your action here
}

只需将self.createAlert()实现为您的主要操作即可。或者实现&#34; self.dismissViewControllerAnimated(true,completion:nil)&#34;到你的cancelAction