2012-11-01 102 views
0

我正在爲我的iOS應用程序創建「登錄」和「創建帳戶」窗體。隱藏時,我成功實現了UITextField的滾動。但是,現在我實現了「下一步」按鈕,因爲鍵盤永遠不會被解僱,因此不會調用「UIKeyboardDidShowNotification」。我需要調用keyboardWasShow方法,以便檢查活動的UITextField是否隱藏。向上滾動UITextField

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
    { 
NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

// If active text field is hidden by keyboard, scroll it so it's visible 
// Your application might not need or want this behavior. 
CGRect aRect = self.view.frame; 

CGPoint pointInSuperview = [self.view convertPoint:self.activeField.frame.origin fromView:self.scrollView]; 

aRect.size.height -= kbSize.height; 
//added 10 to y axis because tip of origin was outside of keyboard 
pointInSuperview.y +=20; 

if (!CGRectContainsPoint(aRect, pointInSuperview)) { 
    CGPoint scrollPoint = CGPointMake(0.0, pointInSuperview.y - (kbSize.height -15)); 
    NSLog(@"it is not in the rect"); 
    [self.scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

,我有一個觀察者

​​

和我實現我的下一步按鈕後(見下文)keyboardWasShown方法不調用,所以它永遠不會檢查活動的UITextField是隱藏的。

//functionality for next action 
- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    if (textField == self.emailAddress) { 
    [self.fullName becomeFirstResponder]; 
    [self keyboardWasShown:(NSNotification*)UIKeyboardDidShowNotification]; 


} 
else if (textField == self.fullName) { 
     [self.password becomeFirstResponder]; 
} 
else if (textField == self.password) { 
    [self.confirmPassword becomeFirstResponder]; 
} 
[textField resignFirstResponder]; 
return YES; 
} 

當用戶點擊下一步按鈕時,調用keyboardWasShown的最佳方法是什麼?我試圖讓它成爲一種公共方法,但是當我嘗試手動調用它時,我不斷收到錯誤。

回答

-1

你可能想看看this

如果你的字段的表同樣的邏輯也可以應用到其他案件的arent。

1

避免這種情況的一種方法是在設置下一個響應者之前辭職響應者,這將確保keyboardWasShown通知被調用。例如,根據你的代碼,你可以使用以下命令:

... 
else if (textField == self.fullName) { 
    [self.fullName resignFirstResponder]; 
    [self.password becomeFirstResponder]; 
} 
... 

雖然這聽起來很奇怪,但應該指出的是,鍵盤實際上並沒有消失/重新出現。