Datenshi
Datenshi

Reputation: 1191

Dismiss keyboard and button

i have two things colliding with each other. Each one alone works fine. But i'v no idea how to make them work both. Here is the thing, i have some textfields and buttons, i'v added dismiss keyboard when user touches background, but this method gets called also then the user tries to click the button.. So the button doesn't work anymore. Here is some code of my keyboard dismiss, maybe anyone will know how to implement this correctly.

-(void)dismissKeyboard {
    NSLog(@"resign");
    [[_textFields objectAtIndex:0] resignFirstResponder];
    [[_textFields objectAtIndex:1] resignFirstResponder];
    [[_textFields objectAtIndex:2] resignFirstResponder];
    [[_textFields objectAtIndex:3] resignFirstResponder];
}


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

[TableControll.view addSubview:save];
[button addTarget:self action:@selector(goAdvanced)
 forControlEvents:UIControlEventTouchUpInside];
[save addTarget:self action:@selector(saveInfo)
     forControlEvents:UIControlEventTouchUpInside];

Upvotes: 1

Views: 162

Answers (5)

Neelam Verma
Neelam Verma

Reputation: 3274

Instead of addGestureRecognizer you can use touch begin functions: For example:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"resign");
    [textfield1 resignFirstResponder];
    [textFiled2 resignFirstResponder];

}

Hope this will help you. Thanks

Upvotes: 0

Vinodh
Vinodh

Reputation: 5268

In the below delegate method

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

include condition like

 if([touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }

and other controls you need to activate touch

Upvotes: 0

Nookaraju
Nookaraju

Reputation: 1668

may be this is because of gestureRecognizer, to differentiate the tap event and button Event use this .

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch {

    if ([touch.view isKindOfClass:[UIButton class]]) {
        return NO;
    }
    return YES;
}

Upvotes: 1

NiravPatel
NiravPatel

Reputation: 3260

Hi just add one button behind that buttons, and that button width will be 320 and height will be 480 according iphone size now you have one button in background and other buttons on that buttons.so now you can give different methods to these buttons.Try it and tell me whether it is working or not.

Happy Coding!!!!!!!

Upvotes: 0

P.J
P.J

Reputation: 6587

Try modifying this part of your code

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];
tap.cancelsTouchesInView = NO;
[TableControll.view addGestureRecognizer:tap];

Hope it helps you!

Upvotes: 2

Related Questions