Question

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");
}
Was it helpful?

Solution 2

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];
}

OTHER TIPS

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];}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

Try to use

[self.view setEditing:YES];

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

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];
}

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top