2016-08-09 3 views
1

私のalertcontrollerのコードは以下の通りです これを行うには、textfield.howを入力すると、通常のキーボードの代わりに数字パッドが表示されます。アラートコントローラのテキストフィールドキーボードを数値パッドに変更するにはどうすればよいですか?

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() 


} 

答えて

2

にはキーボードタイプが変更されています。 あなたのコードを下記に置き換えてください。

alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in 
    textField.keyboardType = UIKeyboardType.NumberPad 
    textField.placeholder = "Password" 
    textField.secureTextEntry = true 
} 
関連する問題