2つのセルを持つテーブルビューがあります。両方のセルには、テキストフィールドとラベルがあります。テキストフィールドユーザー名/電子メールアドレスとパスワードの値を取得します。以下ここでテーブルビューのセルからテキストフィールドの値を取得する方法
var emailAddress: String?
var password: String?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! TextFieldCell
cell.label.text = loginOptions[indexPath.row]
cell.textField.placeholder = loginOptions[indexPath.row]
if indexPath.row == 0 {
emailAddress = cell.textField.text!
}
if indexPath.row == 1 {
password = cell.textField.text!
}
return cell
}
テキストボックスに任意のテキストがあるかどうかをチェックし、そうでない場合は、すべてのフィールドに記入することを示唆している警告、コントローラを表示する機能です。
func signInButtonTapped() {
if emailAddress!.isEmpty || password!.isEmpty {
let alert = UIAlertController(title: "Alert", message: "all fields are required to be filled in", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
}
}
セル内からテキストフィールドの値を取得して空であるかどうかを確認するにはどうすればよいですか?上記のコードは、テキストフィールドが常に空になるため機能しません。が、うまくいけば、私があなたの現在のクラスにUITextFieldDelegate
class ViewController: UIViewController,UITextFieldDelegate
ステップ-2
を代理人を追加
テーブルビューがロードされたときに、上記のコードが実行を行うことができます。その時までにtextFieldは空です。 UITextFieldDelegateを使用して、編集または終了時にtextFieldの動的変化を観察する必要があります。 – ronan