[英]XCode TextField Hide Keyboard
在我的應用程序中,我有不同的TextFields
例如Username
, Name
, Password
等。
因為我將以下textfields
年齡”,“性別”和“國家/地區”用作ActionSheet
選擇器。 我面臨的問題是,當我鍵入password
字段並選擇年齡時,第一次單擊本身並沒有隱藏鍵盤。 該鍵盤隱藏后,我必須再次單擊“年齡”字段。
誰能幫我解決這個問題
- (BOOL)textFieldShouldBeginEditing:(LRTextField *)textField {
NSLog(@"Tag %ld",(long)textField.tag);
if (textField.tag == 3){
return NO;
} else if (textField.tag == 4){
return NO;
} else if (textField.tag==5){
return NO;
} else if (textField.tag==6){
return NO;
}
return YES;
}
我正在使用LTTextField
進行textfield
驗證。
請按照以下步驟
在該pickerview文本字段上做一個簡單的按鈕
添加動作
- (IBAction)hidekey:(id)sender {
[self.view endEditing:YES];
[_yourpickerview setHidden:NO];
}
這是所有文本字段的resignFirstResponder
因此,無論何時您在該textfield
鍵盤上移動,都將隱藏並顯示pickerview
希望為您工作
您可以使用resignFirstResponder
:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField.tag = passwordTag {
textField.resignFirstResponder()
}
return true
}
我遇到了同樣的問題,我編寫了這小段代碼,它們在每個iPhone的正確位置移動。 在您的viewDidLoad中:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardWillHideNotification
object:nil];
之后:
- (void)keyboardDidShow: (NSNotification *) notif{
CGSize keyboardsize = [[[notif userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
long moveScreen = ((long)(keyboardsize.height) - (long)(self.view.frame.size.height) + 465) * -1;
if (moveScreen > 0)
moveScreen = 0;
self.view.frame = CGRectMake(rect.origin.x,moveScreen,rect.size.width,rect.size.height);
[UIView commitAnimations];
}
- (void)keyboardDidHide: (NSNotification *) notif{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
self.view.frame = rect;
[UIView commitAnimations];
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.emailField {
self.usernameField.becomeFirstResponder()
}
else if textField == self.usernameField {
self.nameField.becomeFirstResponder()
}
else if textField == self.nameField {
self.passwordField.becomeFirstResponder()
}
else if textField == self.passwordField {
self.ageField.becomeFirstResponder()
self.ageField.resignFirstResponder();
}
return true;
}
讓ageTextField成為第一響應者,然后辭職第一響應者
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.