2017-07-25 48 views
0

我已經實現了帶有兩個文本字段的警報窗口。我想執行一些驗證:如果其中一個文本字段爲空(用戶沒有輸入任何值),應用程序不應允許用戶按「完成」按鈕。 我不想顯示任何警報,只是不想讓用戶保存空白數據。Swift3 UITextField在按下按鈕後進行UIAlert驗證

我已經創建了下面列出的功能。增加了兩名警衛與回報。但在這種情況下,如果用戶沒有輸入任何內容,則警報會關閉並且不會保存任何內容。我不想讓警報窗口關閉。

如果可以做到的話,該怎麼辦?還沒有找到答案。我檢查了this的問題,但看起來它不適用於我。感謝你的幫助!

private func addTable() { 
    let alert = UIAlertController(title: NSLocalizedString("inputTableParams", comment: ""), message: nil, preferredStyle: .alert) 
    alert.addTextField(configurationHandler: configureTableNameTextField) 
    alert.addTextField(configurationHandler: configureTableCapacityTextField) 
    alert.textFields?[0].autocapitalizationType = .sentences 
    alert.textFields?[1].autocapitalizationType = .sentences 
    alert.addAction(UIAlertAction(title: NSLocalizedString("alertCancel", comment: ""), style: .cancel, handler: nil)) 
    alert.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .default, handler: { (UIAlertAction) in 
     guard self.tableNameTextField.text != "" else {return} 
     guard self.tableCapacityTextField.text != "" else {return} 
     let newTable = Table(tableName: self.tableNameTextField.text!, tableCapacity: Int(self.tableCapacityTextField.text!)!) 
     let result = try? TablesTable.getOrCreateTable(table: newTable) 
     if result != nil { 
      self.updateTableView() 
     } 
    })) 
    self.present(alert, animated: true, completion: {}) 
} 

回答

0

它看起來不可能做到我想要的,所以我實現了另一個警告窗口,並報錯。

//MARK: Functions 
//Functions for Alert window for adding table 
private func configureTableNameTextField (textField: UITextField!) { 
    textField.placeholder = NSLocalizedString("tableName", comment: "") 
    textField.keyboardType = .default 
    tableNameTextField = textField 
} 
private func configureTableCapacityTextField (textField: UITextField!) { 
    textField.placeholder = NSLocalizedString("tableCapacity", comment: "") 
    textField.keyboardType = .numberPad 
    tableCapacityTextField = textField 
} 

private func showAlertParamsNotFilledProperly() { 
    let alertNoCanDo = UIAlertController(title: NSLocalizedString("alertNoCanDo", comment: ""), message: NSLocalizedString("paramsNotFilledProperly", comment: ""), preferredStyle: .alert) 
    alertNoCanDo.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .cancel, handler: nil)) 
    self.present(alertNoCanDo, animated: true, completion: {}) 
} 

private func showAlertUnableToSave() { 
    let alertNoCanDo = UIAlertController(title: NSLocalizedString("alertUnableToSaveData", comment: ""), message: NSLocalizedString("checkInputParameters", comment: ""), preferredStyle: .alert) 
    alertNoCanDo.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .cancel, handler: nil)) 
    self.present(alertNoCanDo, animated: true, completion: {}) 
} 

//Functions for managing tables 
private func addTable() { 
    let alert = UIAlertController(title: NSLocalizedString("inputTableParams", comment: ""), message: nil, preferredStyle: .alert) 
    alert.addTextField(configurationHandler: configureTableNameTextField) 
    alert.addTextField(configurationHandler: configureTableCapacityTextField) 
    alert.textFields?[0].autocapitalizationType = .sentences 
    alert.textFields?[1].autocapitalizationType = .sentences 
    alert.addAction(UIAlertAction(title: NSLocalizedString("alertCancel", comment: ""), style: .cancel, handler: nil)) 
    alert.addAction(UIAlertAction(title: NSLocalizedString("alertDone", comment: ""), style: .default, handler: { (UIAlertAction) in 
     if self.tableNameTextField.text == "" || self.tableCapacityTextField.text == "" { 
      self.showAlertParamsNotFilledProperly() 
      return 
     } 
     if let number = NumberFormatter().number(from: self.tableCapacityTextField.text!) { 
      let capacity = Int(number) 
      let newTable = Table(tableName: self.tableNameTextField.text!, tableCapacity: capacity) 
      let result = try? TablesTable.getOrCreateTable(table: newTable) 
      if result != nil { 
       self.updateTableView() 
      } 
     } else { 
      self.showAlertParamsNotFilledProperly() 
      return 
     } 
    })) 
    self.present(alert, animated: true, completion: {}) 
}