Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between the value changed and editing changed event of UITextField

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

like image 765
inatu Avatar asked Apr 03 '15 02:04

inatu


People also ask

What is text field delegate?

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.


2 Answers

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.

like image 79
Dylan Avatar answered Nov 04 '22 09:11

Dylan


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.

like image 4
fractious Avatar answered Nov 04 '22 08:11

fractious