teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

notificationを使ったやり方に変更

2017/01/13 12:22

投稿

tarofess
tarofess

スコア127

title CHANGED
File without changes
body CHANGED
@@ -59,4 +59,44 @@
59
59
  return !text.isEmpty ? true : false
60
60
  }
61
61
  }
62
+ ```
63
+
64
+ ///notificationを使ったやり方に変更///
65
+
66
+ ```swift
67
+ func didChangeNotification(notification: Notification) {
68
+ var noEmptyTextField = true
69
+
70
+ if let textField = notification.object as? UITextField {
71
+ switch textField.tag {
72
+ case CellTag.name.rawValue:
73
+ if let text = textField.text, !text.isEmpty {
74
+ noEmptyTextField = true
75
+ } else {
76
+ noEmptyTextField = false
77
+ }
78
+ case CellTag.category.rawValue:
79
+ if let text = textField.text, !text.isEmpty {
80
+ noEmptyTextField = true
81
+ } else {
82
+ noEmptyTextField = false
83
+ }
84
+ case CellTag.price.rawValue:
85
+ if let text = textField.text, !text.isEmpty {
86
+ noEmptyTextField = true
87
+ } else {
88
+ noEmptyTextField = false
89
+ }
90
+ default:
91
+ break
92
+ }
93
+ }
94
+
95
+ if noEmptyTextField {
96
+ self.navigationItem.rightBarButtonItem?.isEnabled = true
97
+ } else {
98
+ self.navigationItem.rightBarButtonItem?.isEnabled = false
99
+ }
100
+ }
101
+
62
102
  ```

2

タイトル編集

2017/01/13 12:22

投稿

tarofess
tarofess

スコア127

title CHANGED
@@ -1,1 +1,1 @@
1
-
1
+ 全てのTextFieldが空の時を判別した
body CHANGED
File without changes

1

回答を受けての追記

2017/01/12 23:42

投稿

tarofess
tarofess

スコア127

title CHANGED
@@ -1,1 +1,1 @@
1
- 全てのTextFieldが空の時を判別した
1
+
body CHANGED
@@ -30,4 +30,33 @@
30
30
 
31
31
  ```
32
32
  どうすれば入力と同時にtextfieldが空なのかどうかを判別することができるでしょうか?
33
- どなたかわかる方がいれば教えていただきたいです。よろしくお願いします。
33
+ どなたかわかる方がいれば教えていただきたいです。よろしくお願いします。
34
+
35
+ ///回答を受けての追記///
36
+
37
+ ```swift
38
+ func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
39
+ let newText = (textField.text as! NSString).replacingCharacters(in: range, with: string)
40
+
41
+ let filledTextFieldCells = self.tableView.visibleCells.filter {
42
+ if let cell = $0 as? Checkable {
43
+ return cell.isFilledTextField(text: newText)
44
+ } else {
45
+ return false
46
+ }
47
+ }
48
+ if filledTextFieldCells.count == 0 {
49
+ self.navigationItem.rightBarButtonItem?.isEnabled = false
50
+ } else {
51
+ self.navigationItem.rightBarButtonItem?.isEnabled = true
52
+ }
53
+
54
+ return true
55
+ }
56
+
57
+ extension Checkable {
58
+ func isFilledTextField(text: String) -> Bool {
59
+ return !text.isEmpty ? true : false
60
+ }
61
+ }
62
+ ```