1

I have one TableViewCell that has UITextField in it. I want when click out of UITextField hidden keyboard but I don't about that.

this is my code:

@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 Answers6

3

Add gesture recogniser in viewDidLoad :

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

and then implement closeKeyboard:

- (IBAction)closeKeyboard:(id)sender {
[self.view endEditing:YES];}
wootage
  • 936
  • 6
  • 14
  • what is the need of tab gesture recognizer. We can get touch using -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method – Mayank Jain Apr 15 '14 at 13:02
  • If you have some view with scroll it will get the touch event and touchBegan wont be called, so I use tap gesture to keep it more simple – wootage Apr 15 '14 at 13:12
  • ok.. but we can also sub class the scroll view with touch enable. – Mayank Jain Apr 15 '14 at 13:13
  • You want to subclass scroll view just to get the touch event and close the keyboard?Yes you can do it, but I prefer tap gesture – wootage Apr 15 '14 at 13:16
1

my friend I had this problem but I can solve that. you don't need add UITextField in TableViewCell , you can create TextField in ViewController or TableViewController that your tableView is inner it and then add TextField inside any cell that you want.

like this code:

ViewController.h

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

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

ViewController.m

@implementation ViewController
@synthesize table,Textfield;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //add table in view
    table = [self makeTableView];
    [self.view addSubview:table];
    [self.view setBackgroundColor: RGB(193,60,46)]; //will give a UIColor objct

    //run textfield programmatically
    Textfield = [self makeTextField];

    //hide keyboard with hideKeyboard selector
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.table addGestureRecognizer:gestureRecognizer];

}
- (void) hideKeyboard {
    [Textfield resignFirstResponder];
}
MmD
  • 2,044
  • 5
  • 22
  • 41
0
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
0

Try to use

[self.view setEditing:YES];

in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

Fry
  • 6,235
  • 8
  • 54
  • 93
0

try this:

// 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];
}
Mykola Denysyuk
  • 1,935
  • 1
  • 15
  • 15
-1

Add this code in your textField implementation method.

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

        [self.view addGestureRecognizer:tap];

After that create dismissKeyboard method.

//First Responder Remove Keyboard
-(void)dismissKeyboard {
    [Textfield resignFirstResponder];
}
DilumN
  • 2,889
  • 6
  • 30
  • 44