2014-03-25 45 views
0

我在我的應用程序中使用TPKeyboardAvoiding來在鍵盤顯示時隱藏移動文本字段,但在嘗試結束編輯文本字段時出現異常。它來自TPKeyboardAvoiding中的這種方法:發送到實例的無法識別的選擇器resignFirstResponder

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self]; 
    NSLog(@"%@",[view description]); 
    [view resignFirstResponder]; //this line gives the exception 
    [super touchesEnded:touches withEvent:event]; 
} 

我在這裏有點困惑。不應該所有的UIViews迴應resignFirstResponder?謝謝您的幫助。

完整的錯誤:

2014-03-25 17:40:39.919 Rysk[5553:70b] -[MenuViewController textFieldDidBeginEditing:]: unrecognized selector sent to instance 0xb63c820 
+0

嘗試此代替:'[self.view endEditing:YES];' – klcjr89

+0

嘗試'如果([視圖isKindOfClass:[UITextField類]] || [視圖isKindOfClass:[UITextView的class]]){[view resignFirstResponder]; }' – Akhilrajtr

+0

@ troop231我也收到了一個異常 – connor

回答

0

我解決了我的視圖控制器比包含TPKeyboardAvoidingScrollView實現UITextFieldDelegate協議的問題。最重要的是,這兩種方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField{} 
- (void)textFieldDidEndEditing:(UITextField *)textField{} 
0

更新您的代碼如下:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self]; 
    NSLog(@"%@",[view description]); 
    if([view isKindOfClass:[UITextField class]]) { 
     UITextField *myTextField = (UITextField *)view; 
     [myTextField resignFirstResponder]; 
    } 
    [super touchesEnded:touches withEvent:event]; 
} 
+0

感謝您的回答。我仍然遇到resignFirstResponder行 – connor

+0

的例外情況。 –

+0

'無法識別的選擇器發送到實例0xb44b1a0' – connor

0

試試這個簡單的方法....

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //--Gesture to resign keyborad on touch outside 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
    tap.cancelsTouchesInView = NO; 
    [self.view addGestureRecognizer:tap]; 
} 


//-- Resign keyboard on touch UIView 
-(void)dismissKeyboard 
{ 
    self.view.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height); 
    [self.view endEditing:YES]; 
} 
+0

試試這個。它會解決你的問題比以前的方法.. – svmrajesh

+0

我應該在哪裏把這段代碼? – connor

+0

您的.m文件的viewDidLoad中的前3行..下一個方法dismissKeyboard放置在代碼的任何位置... – svmrajesh

1

不知道如果你調用[yourTextField resignFirstResponder]以及。因此,有可能UITextField(在您提供的代碼中)不是此時的第一個響應者。我會建議調整你這樣的代碼:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self]; 

    if([view conformsToProtocol:@protocol(UITextFieldDelegate)] || [view conformsToProtocol:@protocol(UITextViewDelegate)]) && 
     [view isFirstResponder] && [view canResignFirstResponder]) 
    { 
     [view resignFirstResponder]; 
    } 

    [super touchesEnded:touches withEvent:event]; 
} 

另外,如果你使用的POD請確保您使用的是最新的版本,因爲我用的是一個在這種情況下是這樣的:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [[self TPKeyboardAvoiding_findFirstResponderBeneathView:self] resignFirstResponder]; 
    [super touchesEnded:touches withEvent:event]; 
} 

希望它有幫助!

+0

謝謝你的回答。儘管如此,我仍然遇到了無法識別的選擇器錯誤。我的代碼過去也是這樣,我已經改變了一下,試圖找出錯誤的位置。 – connor

+0

你也自己辭職嗎?我是說這個班以外?檢查我的更新以及 – NeverHopeless

+0

如果它可以resignFirstResponder,我仍然會得到相同的異常檢查。調用resignFirstResponder的唯一地方是在該類中 – connor

相關問題