我知道这个问题已被问过几次,但没有一个答案对我(me = n00b)有足够的细节可以理解。
我有一个简单的小应用程序,我通过故事板放在一起,它有两个文本字段,用户可以键入。我希望用户在编辑任一字段后能够关闭键盘。
我知道这与“resignFirstResponder”有关,但我不知道该把它放在哪里。
这是我的最高机密代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize thing1;
@synthesize thing2;
// NSArray things = @[thing1, thing2];
// self.thingLabel.text = array[arc4random()%array.count];
- (IBAction)pick:(id)sender {
// create an empty list that will get filled with things
NSMutableArray *things = [NSMutableArray arrayWithObjects:thing1.text,thing2.text, nil];
NSString *theThing = things[arc4random()%things.count];
NSLog(@"the thing is %@", theThing);
}
@end
这是我的ViewController.h文件:
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *thing1;
@property (weak, nonatomic) IBOutlet UITextField *thing2;
@end
答案 0 :(得分:1)
由于您的视图控制器采用UITextFieldDelegate
协议(即<UITextFieldDelegate>
),因此最简单的方法是让视图控制器实现以下两种方法:
// place in ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// note that you could set the text field delegate in IB instead
self.thing1.delegate = self;
self.thing2.delegate = self;
}
// this method gets called by the system automatically when the user taps the keyboard's "Done" button
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
[theTextField resignFirstResponder];
return YES;
}
这就是你需要的所有代码。现在,当用户点击键盘的“完成”按钮时,键盘就会消失。
答案 1 :(得分:0)
您可以在文本字段旁边添加一个按钮,并在按下该按钮时将其关闭:
UIButton *doneEnteringButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneEnteringButton addTarget:addTarget:self action:@selector(dismissKeyboard:) forControlEvents:UIControlEventTouchUpInside];
- (void)dismissKeyboard
{
[self.view endEditing:YES];
}
无论您处于哪个文本字段,此单个按钮都将关闭键盘。如果您不想要按钮,则可以在文本字段的父视图中添加手势识别器并执行{{1}那里。
希望这有帮助!询问它是否不是
答案 2 :(得分:0)
尝试此代码,当用户点按任何解除键盘的位置时
- (void)setupKeyboardDismiss{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
UITapGestureRecognizer *singleTapGR =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapAnywhereToDismissKeyboard:)];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[nc addObserverForName:UIKeyboardWillShowNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification *note){
[self.view addGestureRecognizer:singleTapGR];
}];
[nc addObserverForName:UIKeyboardWillHideNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification *note){
[self.view removeGestureRecognizer:singleTapGR];
}];
}
- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer{
[self.view endEditing:YES];
}
答案 3 :(得分:0)
这取决于UI&amp;您正在尝试开发的功能。您可以通过以下方式完成此操作:
[yourTextField resignFirstResponder]
中执行操作。UITextField
上实施以下委托方法并将逻辑放入其中,并在满足所需条件后调用[yourTextField resignFirstResponder]
。例如,一旦用户输入了一定数量的字符,它将自动关闭键盘。当用户完成并点击文本视图外部时,请关闭键盘。在UIView上实现'touchesbegan'并让viewcontroller了解它。
-(BOOL)textField:(UITextField *)iTextField shouldChangeCharactersInRange:(NSRange)iRange replacementString:(NSString *)iString
-(void)textFieldDidEndEditing:(UITextField *)iTextField