如何将alertcontroller文本字段键盘更改为numberpad?

时间:2016-08-09 14:33:58

标签: ios swift uialertcontroller

以下是我的alertcontroller的代码 当我在textfield中输入时,我想让数字键盘显示而不是普通键盘吗?

func addPasscodeAlert()
{
    let blurEffect = UIBlurEffect(style: .Light)
    let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
    blurVisualEffectView.frame = view.bounds
    self.view.addSubview(blurVisualEffectView)

    let alertController = UIAlertController(title: "No passcode saved", message: "Please enter new 4 digit passcode.", preferredStyle: .Alert)

    let defaultAction = UIAlertAction(title: "OK", style: .Cancel) { (action) -> Void in


        if let textField = alertController.textFields?.first as UITextField?
        {
            textField.keyboardType = UIKeyboardType.NumberPad
            self.passcode = textField.text!
            let userDefaults = NSUserDefaults.standardUserDefaults()
            userDefaults.setObject(self.passcode, forKey: "passcode")
            self.showPasswordAlert()
        }
    }

    alertController.addAction(defaultAction)

    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in

        textField.placeholder = "Password"
        textField.secureTextEntry = true
    }
    self.presentViewController(alertController, animated: true, completion: nil)
    blurVisualEffectView.removeFromSuperview()


}

1 个答案:

答案 0 :(得分:2)

更改处理程序方法中的键盘类型而不是UIAlertAction。 用下面的代码替换你的代码。

alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
    textField.keyboardType = UIKeyboardType.NumberPad
    textField.placeholder = "Password"
    textField.secureTextEntry = true
}
相关问题