您好我發現了一些像這樣的問題,但他們談論textView,我有ViewController,與scrollView其中有6個textfield和一個textView我想要一個功能,使鍵盤消失在完成/返回按鈕click.I實現功能resign第一響應者,它隱藏我的鍵盤,當我點擊scrollView外,但這不完全是我想要的,因爲我想讓它消失在按鈕點擊太。如何隱藏鍵盤在UIViewController返回按鈕click-> iphone
感謝所有幫助
您好我發現了一些像這樣的問題,但他們談論textView,我有ViewController,與scrollView其中有6個textfield和一個textView我想要一個功能,使鍵盤消失在完成/返回按鈕click.I實現功能resign第一響應者,它隱藏我的鍵盤,當我點擊scrollView外,但這不完全是我想要的,因爲我想讓它消失在按鈕點擊太。如何隱藏鍵盤在UIViewController返回按鈕click-> iphone
感謝所有幫助
套裝創建一個符合UITextFieldDelegate協議的類,並使您的文本字段的委託成爲此類的一個實例。實施方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
如下:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
嗨,我發現出來,所以用文本框的一點是在viewDidLoad中添加此行:
textFieldOne.returnKeyType = UIReturnKeyDone;
textFieldCislo.delegate = self;
textFieldTwo.returnKeyType = UIReturnKeyDone;
textFieldCislo.delegate = self;
...
而這種實現方法:
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textFieldOne) {
[textFieldOne resignFirstResponder];
}
...
}
相當多的時間追捕這是令後感,這是我放在一起,它的工作就像一個魅力。
.H
//
// ViewController.h
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;
@end
的.m
//
// ViewController.m
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// _theTextField is the name of the parameter designated in the .h file.
_theTextField.returnKeyType = UIReturnKeyDone;
[_theTextField setDelegate:self];
}
// This part is more dynamic as it closes any text field when pressing return.
// You might want to control every single text field separately but that isn't
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
@end
U可以通過點擊視圖中的任何地方使用這種方法來隱藏鍵盤
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
添加一個「返回YES; 「在底部去除警告。還要在界面中添加「」以刪除更多警告。 –
Sunkas
2013-01-10 09:17:48