質問編集履歴
3
notificationを使ったやり方に変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -121,3 +121,83 @@
|
|
121
121
|
}
|
122
122
|
|
123
123
|
```
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
///notificationを使ったやり方に変更///
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
```swift
|
132
|
+
|
133
|
+
func didChangeNotification(notification: Notification) {
|
134
|
+
|
135
|
+
var noEmptyTextField = true
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
if let textField = notification.object as? UITextField {
|
140
|
+
|
141
|
+
switch textField.tag {
|
142
|
+
|
143
|
+
case CellTag.name.rawValue:
|
144
|
+
|
145
|
+
if let text = textField.text, !text.isEmpty {
|
146
|
+
|
147
|
+
noEmptyTextField = true
|
148
|
+
|
149
|
+
} else {
|
150
|
+
|
151
|
+
noEmptyTextField = false
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
case CellTag.category.rawValue:
|
156
|
+
|
157
|
+
if let text = textField.text, !text.isEmpty {
|
158
|
+
|
159
|
+
noEmptyTextField = true
|
160
|
+
|
161
|
+
} else {
|
162
|
+
|
163
|
+
noEmptyTextField = false
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
case CellTag.price.rawValue:
|
168
|
+
|
169
|
+
if let text = textField.text, !text.isEmpty {
|
170
|
+
|
171
|
+
noEmptyTextField = true
|
172
|
+
|
173
|
+
} else {
|
174
|
+
|
175
|
+
noEmptyTextField = false
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
default:
|
180
|
+
|
181
|
+
break
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
if noEmptyTextField {
|
190
|
+
|
191
|
+
self.navigationItem.rightBarButtonItem?.isEnabled = true
|
192
|
+
|
193
|
+
} else {
|
194
|
+
|
195
|
+
self.navigationItem.rightBarButtonItem?.isEnabled = false
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
```
|
2
タイトル編集
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
全てのTextFieldが空の時を判別したい
|
test
CHANGED
File without changes
|
1
回答を受けての追記
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
かい
|
test
CHANGED
@@ -63,3 +63,61 @@
|
|
63
63
|
どうすれば入力と同時にtextfieldが空なのかどうかを判別することができるでしょうか?
|
64
64
|
|
65
65
|
どなたかわかる方がいれば教えていただきたいです。よろしくお願いします。
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
///回答を受けての追記///
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
```swift
|
74
|
+
|
75
|
+
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
76
|
+
|
77
|
+
let newText = (textField.text as! NSString).replacingCharacters(in: range, with: string)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
let filledTextFieldCells = self.tableView.visibleCells.filter {
|
82
|
+
|
83
|
+
if let cell = $0 as? Checkable {
|
84
|
+
|
85
|
+
return cell.isFilledTextField(text: newText)
|
86
|
+
|
87
|
+
} else {
|
88
|
+
|
89
|
+
return false
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
if filledTextFieldCells.count == 0 {
|
96
|
+
|
97
|
+
self.navigationItem.rightBarButtonItem?.isEnabled = false
|
98
|
+
|
99
|
+
} else {
|
100
|
+
|
101
|
+
self.navigationItem.rightBarButtonItem?.isEnabled = true
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
return true
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
extension Checkable {
|
114
|
+
|
115
|
+
func isFilledTextField(text: String) -> Bool {
|
116
|
+
|
117
|
+
return !text.isEmpty ? true : false
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
```
|