如何在点击背景时隐藏键盘

时间:2014-04-15 12:39:08

标签: ios objective-c uitableview uitextfield

我有一个TableViewCell,其中包含UITextField。 我想点击UITextField隐藏键盘,但我不知道。

这是我的代码:

@implementation TextFieldCell
@synthesize Textfield,placeholder;
- (void)awakeFromNib
{
    // Initialization code
    Textfield = [self makeTextField];
    [self addSubview:Textfield];

    placeholder = [self PlaceHolderLabel];
    [self addSubview:placeholder];

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (UITextField *)makeTextField
{
    CGRect TextFrame = CGRectMake(0,0,300,80);
    UITextField *textfield = [[UITextField alloc]initWithFrame:TextFrame];
    textfield.delegate = self;
    textfield.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
    textfield.backgroundColor = [UIColor yellowColor];
    textfield.placeholder = [NSString stringWithFormat:@"Mamal"];
    UIView *spacerleftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
    UIView *spacerrightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
    [textfield setLeftViewMode:UITextFieldViewModeAlways];
    [textfield setRightViewMode:UITextFieldViewModeAlways];
    textfield.leftView = spacerleftView;
    textfield.rightView = spacerrightView;


    return textfield;
}
- (UILabel *)PlaceHolderLabel{

    NSString *labelText = Textfield.placeholder;
    Textfield.placeholder = nil;
    CGRect labelFrame = CGRectMake(0,0,280,80);
    UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
    [placeholderLabel setTextColor:[UIColor lightGrayColor]];
    [placeholderLabel setText:labelText];
    placeholderLabel.textAlignment = NSTextAlignmentRight;
    return placeholderLabel;
}
- (UILabel *)clearPlaceHolderLabel{

    NSString *labelText = nil;
    CGRect labelFrame = CGRectMake(0,0,280,80);
    UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
    [placeholderLabel setText:labelText];
    return placeholderLabel;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    //Keyboard becomes visible
    //perform actions.
    NSLog(@"start");
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"end");
}

6 个答案:

答案 0 :(得分:3)

在viewDidLoad中添加手势识别器:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];
[self.view addGestureRecognizer:tap];

然后实施closeKeyboard:

- (IBAction)closeKeyboard:(id)sender {
[self.view endEditing:YES];}

答案 1 :(得分:1)

我的朋友我有这个问题,但我可以解决这个问题。 您不需要在UITextField中添加TableViewCell,您可以在TextFieldViewController中创建{table}内部的TableViewController,然后添加{ {1}}在您想要的任何单元格内。

喜欢这段代码:

<强> ViewController.h

TextField

<强> ViewController.m

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UITableView *table;
@property (weak,nonatomic) UITextField *Textfield;

答案 2 :(得分:0)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

答案 3 :(得分:0)

尝试使用

[self.view setEditing:YES];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

中的

答案 4 :(得分:0)

试试这个:

// somewhere in viewDidLoad
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnBackground)];
[self.view addGestureRecognizer:tapRecognizer];
self.view.userInteractionEnabled = YES;
// ...

- (void)tapOnBackground
{
    [self.view endEditing:YES];
}

答案 5 :(得分:-1)

在textField实现方法中添加此代码。

 //Set to Remove Keyboard from view
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(dismissKeyboard)];

        [self.view addGestureRecognizer:tap];

之后创建dismissKeyboard方法。

//First Responder Remove Keyboard
-(void)dismissKeyboard {
    [Textfield resignFirstResponder];
}