2014-01-16 27 views
0

我試圖在鍵盤出現時將視圖向上移動,然後再次消失。 第一次出現鍵盤時,它按預期工作。當按'完成'時,鍵盤消失並且視圖移回。但是,在初始編輯之後,單擊文本文件不會執行任何操作。鍵盤根本沒有出現。UITextField:第一次編輯後鍵盤不出現

viewDidLoad方法我已經寫了:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; 

並實施了以下代表:

- (void)keyboardDidShow:(NSNotification *)notification 
{ 
    //Assign new frame to your view 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    NSDictionary* info = [notification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    [self.view setFrame:CGRectMake(0,-kbSize.height,320,460)]; 

    [UIView commitAnimations]; 

} 
-(BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [self.view endEditing:YES]; 
    return NO; 
} 

-(void)keyboardDidHide:(NSNotification *)notification 
{ [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [self.view setFrame:CGRectMake(0,0,320,460)]; 
    [UIView commitAnimations]; 
} 

回答

0

您需要實現您的UITextField resignFirstResponder使鍵盤可以再次啓動。在你實現你可以做的是以下行添加到您的textFieldShouldReturn方法:

[textfield resignFirstResponder]; 
0

如果您改變-textFieldShouldReturn方法如下它應該工作。

-(BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 
} 

每文檔:

通過返回NO,您可以防止用戶切換到另一個控制

沒有返回,一直專注於控制,所以我假設下次您點擊textFiled時什麼都不會發生,因爲系統認爲控件已經集中。