2017-01-23 73 views
0

我已經創建了UIAlertController和UITextField,而且UITextField只需要取0-100之間的整數。如何限制UIAlertController上的UITextField只接受指定範圍的數字?

我該怎麼做?

這是我的代碼

let confirm = UIAlertController(title: "I want to reserve", message: "sometext", preferredStyle: UIAlertControllerStyle.alert) 

      confirm.addTextField(configurationHandler: { (textField) in 


       textField.placeholder = "Please fill the number between 0-100" 

      }) 

      confirm.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil)) 

      confirm.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.destructive, handler: { (action) in 

       self.isReserve = true 

       self.reservedIcon.alpha = 0 
       self.reservedTitle.alpha = 0 

       self.cancelReservedIcon.alpha = 1 
       self.cancelReservedTitle.alpha = 1 


      })) 

回答

0
let confirm = UIAlertController(title: "I want to reserve", message: "sometext", preferredStyle: UIAlertControllerStyle.alert) 

     confirm.addTextField(configurationHandler: { (textField) in 


      textField.placeholder = "Please fill the number between 0-100" 

     }) 

     confirm.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil)) 

     confirm.addAction(UIAlertAction(title: "Yes", style: .default, handler: { [weak alert] (_) in 
     let textField = alert?.textFields![0] 
     if textField.text != "1234" { // change your if condition 
      self.isReserve = true 

      self.reservedIcon.alpha = 0 
      self.reservedTitle.alpha = 0 

      self.cancelReservedIcon.alpha = 1 
      self.cancelReservedTitle.alpha = 1 
     } else { 
      // your else condition 
     } 
})) 
0

添加UITextFieldDelegate到控制器

那麼你的代碼:

let confirm = UIAlertController(title: "I want to reserve", message: "sometext", preferredStyle: UIAlertControllerStyle.alert) 

     confirm.addTextField(configurationHandler: { (textField) in 


      textField.placeholder = "Please fill the number between 0-100" 

     }) 
     let textfield = confirm.textFields?.first 
     textfield?.keyboardType = .numberPad 
     textfield?.delegate = self 
     confirm.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil)) 

     confirm.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.destructive, handler: { (action) in 

      self.isReserve = true 

      self.reservedIcon.alpha = 0 
      self.reservedTitle.alpha = 0 

      self.cancelReservedIcon.alpha = 1 
      self.cancelReservedTitle.alpha = 1 


     })) 

     self.present(confirm, animated: true) { 

     } 

添加文本字段代表:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    guard let text = textField.text else { return true } 
    return text.characters.count <= 100 // Your limit 
} 
0

可以使用shouldChangeCharactersInRange方法和字符串擴展到檢查輸入字符串中包含數和控制0之間的文本輸入到100

extension String { 
     var isNumber : Bool { 
      get{ 
       return !self.isEmpty && self.stringWithoutWhitespaces.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil 
      } 
     } 

     var stringWithoutWhitespaces: String { 
      return self.replacingOccurrences(of: " ", with: "") 
     } 

    } 


    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
     // return true if the string only contains numeric characters 
     var isValid = false 
     if string.isNumber { 
      if let num = Int(string.stringWithoutWhitespaces) { 
       isValid = (num >= 0 && num <= 100) 
      } 
     } 
     return isValid 
    } 
相關問題