Reputation: 283
I am currently reading a book about design patterns in swift, and there is a program that a method get notified when a stepper's value changed or a text field associated with it changed, here is the method
@IBAction func stockLevelDidChange(sender: AnyObject) {
println("Method Trigged")
if var currentCell = sender as? UIView {
while (true) {
currentCell = currentCell.superview!;
if let cell = currentCell as? ProductTableViewCell {
if let id = cell.productID? {
var newStockLevel:Int?;
if let stepper = sender as? UIStepper {
newStockLevel = Int(stepper.value);
} else if let textfield = sender as? UITextField {
if let newValue = textfield.text.toInt()? {
newStockLevel = newValue;
}
}
if let level = newStockLevel {
products[id].4 = level;
cell.stockStepper.value = Double(level);
cell.stockField.text = String(level);
}
}
break;
}
}
displayTotalStock();
}
}
but I have some problems when change this thread of code, first when I just stripped out the while look, it just did not work.
@IBAction func stockLevelDidChange(sender: AnyObject) {
println("Method Trigged")
if var currentCell = sender as? UIView {
currentCell = currentCell.superview!;
if let cell = currentCell as? ProductTableViewCell {
if let id = cell.productID? {
var newStockLevel:Int?;
if let stepper = sender as? UIStepper {
newStockLevel = Int(stepper.value);
} else if let textfield = sender as? UITextField {
if let newValue = textfield.text.toInt()? {
newStockLevel = newValue;
}
}
if let level = newStockLevel {
products[id].4 = level;
cell.stockStepper.value = Double(level);
cell.stockField.text = String(level);
}
}
break;
}
displayTotalStock();
}
}
and also, when I changed the text field's event from editing changed to value changed, it just don't work too!!!
anyone knows what's going on there, thanks
Upvotes: 12
Views: 6156
Reputation: 1662
From Apple's UIControl Control Events docs:
UIControlEventValueChanged
A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.
UIControlEventEditingChanged
A touch making an editing change in a UITextField object.
Upvotes: 6
Reputation: 104
Editing change is the textField statu. Value Changed is textField content has changed.
if you want do some operation, you can use
Such:
[textField addTarget:self action:@selector(textFieldAction:) forControlEvents:UIControlEventValueChanged];
you can change Control Event for your target. Action. Thanks.
Upvotes: 8