I send the willChangeValueForKey: and didChangeValueForKey:, but the UI doesn't update to the new values while that text field is still active.
वहाँ बहुत कुछ कारणों से उन संदेश भेजने के लिए कर रहे हैं। आम तौर पर, आप एक्सेसर्स (या बेहतर, अभी तक गुणों) को लागू करने और उपयोग करके एक ही काम बेहतर और अधिक साफ कर सकते हैं। जब आप ऐसा करते हैं तो केवीओ आपके लिए अधिसूचनाएं भेजेगा।
आपके मामले में, आप या तो अस्वीकार या फर्जी आदानों को फिल्टर ("12abc" की तरह) करना चाहते हैं। इस कार्य के लिए सही उपकरण कुंजी-मान सत्यापन है।
इस सक्षम करने के लिए आईबी में बंधन पर "पुष्टि तुरंत" बॉक्स को चेक करें, और एक सत्यापन विधि को लागू।
छनन:
- (BOOL) validateMyValue:(inout NSString **)newValue error:(out NSError **)outError {
NSString *salvagedNumericPart;
//Determine whether you can salvage a numeric part from the string; in your example, that would be “12”, chopping off the “abc”.
*newValue = salvagedNumericPart; //@"12"
return (salvagedNumericPart != nil);
}
को अस्वीकार करते हुए:
- (BOOL) validateMyValue:(inout NSString **)newValue error:(out NSError **)outError {
BOOL isEntirelyNumeric;
//Determine whether the whole string (perhaps after stripping whitespace) is a number. If not, reject it outright.
if (isEntirelyNumeric) {
//The input was @"12", or it was @" 12 " or something and you stripped the whitespace from it, so *newValue is @"12".
return YES;
} else {
if (outError) {
*outError = [NSError errorWithDomain:NSCocoaErrorDomain code: NSKeyValueValidationError userInfo:nil];
}
//Note: No need to set *newValue here.
return NO;
}
}
(I've also noticed that the setter method receives an NSString, not an NSNumber. Is that normal?)
हाँ, जब तक आप एक मूल्य के ट्रांसफार्मर कि संख्या में तार बदल देती है का उपयोग करें, एक नंबर फ़ॉर्मेटर formatter
आउटलेट से, कनेक्ट या स्थानापन्न आपकी सत्यापन विधि में एनएसएसटींग के लिए एक NSNumber।
स्रोत
2009-02-09 09:03:23
मैं के रूप में डॉक में आप ने कहा में सुझाव दिया, लेकिन कुछ नहीं होता पाठ क्षेत्र पर छोड़ने के द्वारा एक नंबर फ़ॉर्मेटर गयी। मैं अभी भी गैर संख्यात्मक वर्ण टाइप कर सकता हूं। क्या मुझे फॉर्मेटर को टेक्स्ट फ़ील्ड से जोड़ने के अलावा कुछ और करने की ज़रूरत है? – lajos