2017-07-25 4 views
0

2つのテキストフィールドを持つ警告ウィンドウを実装しました。そして、私はいくつかのバリデーションを実行したい:テキストフィールドの1つが空白の場合(ユーザーが値を入力しなかった場合)、アプリケーションは「完了」ボタンを押してはいけません。 アラートを表示したくない場合は、空のデータを保存しないようにしてください。Swift3 UITextField in UIAlertボタンを押した後の検証

以下の機能を作成しました。 2人のガードをリターンで追加しました。しかし、この場合、ユーザーが何も入力しなかった場合、アラートが閉じて何も保存されません。私は警告窓を閉めたくない。

できる場合はどうすればできますか?答えは見つかりませんでした。私は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: {}) 
} 
関連する問題