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
A text field calls the methods of its delegate in response to important changes. You use these methods to validate text that was typed by the user, to respond to specific interactions with the keyboard, and to control the overall editing process.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With