2013-12-17 3 views
16

इसलिए मैंने कीबोर्ड उपस्थिति ईवेंट के लिए एक अधिसूचना स्थापित की है। अब हम एक UITextView और एक UITextField पर विचार करें।यूआईटीएक्स्टफिल्ड और कीबोर्ड नोटिफिकेशन - अजीब ऑर्डर

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow:) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

चयनकर्ता है:

- (void)keyboardWillShow:(NSNotification *)notification { 

     keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
} 

एक UITextView के मामले में, प्रतिनिधि विधि - (void)textViewDidBeginEditing:(UITextView *)textViewkeyboardWillShow: विधि के बाद निकाल दिया जाता है। तो कीबोर्ड आकार में कीबोर्ड का वास्तविक आकार होता है और मैं इसे टेक्स्टव्यू प्रतिनिधि विधि के अंदर उपयोग करने में सक्षम हूं।

हालांकि एक UITextField के मामले में, इसी प्रतिनिधि विधि - (void)textFieldDidBeginEditing:(UITextField *)textFieldkeyboardWillShow: विधि से पहले निकाल दिया गया है।

ऐसा क्यों है? मैं कैसे कीबोर्ड की CGSize पाठ फ़ील्ड के मामले में के रूप में अब यह सिर्फ शून्य क्योंकि पाठ फ़ील्ड प्रतिनिधि पहले कहा जाता है और नहीं कुंजीपटल चयनकर्ता रिटर्न मिलता है।

उत्तर

4

अजीब ... ऐप्पल के अंत में गलती की तरह लगता है।

शायद आप कीबोर्ड पॉपिंग में देरी कर सकते हैं? यहाँ मेरी दुर्भाग्य से बहुत गंदा "काम के आसपास" सुझाव है - जब पाठ क्षेत्र का चयन किया जाता तो आपको एक नोटिफिकेशन भेज सकता है, लेकिन उसके बाद ही वास्तव में संपादन एक दूसरे के एक अंश बाद में इतना है कि पाठ क्षेत्र से पहले keyboardWillShow: कहा जाता है में जाना जाता है वास्तव में शुरू करते हैं। उदाहरण के लिए:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 

    // Notification corresponding to "textFieldSelected:" method 
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TEXT_FIELD_SELECTED object:nil userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:textField, @"textField", nil]]; 

    // "textFieldReallyShouldBeginEditing" is initially set as FALSE elsewhere in the code before the text field is manually selected 
    if (textFieldReallyShouldBeginEditing) 
     return YES; 
    else 
     return NO: 
} 

- (void)textFieldSelected:(NSNotification*)notification { 

    // Done in a separate method so there's a guaranteed delay and "textFieldReallyShouldBeginEditing" isn't set to YES before "textFieldShouldBeginEditing:" returns its boolean. 
    [self performSelector:@selector(startTextFieldReallyEditing:) withObject:(UITextField*)notification[@"textField"] afterDelay:.01]; 
} 

- (void)startTextFieldReallyEditing:(UITextField*)textField { 
    textFieldReallyShouldBeginEditing = YES; 

    // To trigger the keyboard 
    [textField becomeFirstResponder]; 
} 

तो फिर तुम अधिसूचना बना रहे हैं पर निर्भर करता है, तो आप इस अब ज्ञात पाठ क्षेत्र से पहले ही संपादन शुरू होता है के मूल्य में सम्मिलित कर सकते हैं।

+1

मेरे मामले में सरल dispatch_async (mainQueue,^{}) ने मदद की। – Andy

4

मुझे यह समस्या भी आई है। उपयोग करने का प्रयास करें:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView    
+0

प्रश्न टेक्स्टफील्ड के बारे में है और आप टेक्स्टव्यू प्रतिनिधि विधि का सुझाव देते हैं। मुझे यह नहीं मिला, क्या बात है? – Andy

+0

जवाब इस समस्या के लिए अप्रासंगिक हो सकता है, लेकिन यह मेरे मामले में समस्या हल हो जाती। धन्यवाद! –

संबंधित मुद्दे