Code cracker
Code cracker

Reputation: 3166

UIButton accepting only double click

I am showing a UITableView when user tap the UIButton. If the user tapped the UIButton second time UITableView should be off. so i wrote below code but it is accepting only double click.

in .h BOOL selected;

in .m

-(void)ShowTable {
    if (selected)
    {
            aTableview=[[UITableView alloc]initWithFrame:CGRectMake(20, 280, 128, 140)];
            aTableview.dataSource=self;
            aTableview.delegate=self;
            [scrollView addSubview:aTableview];

           [aTableview setHidden:NO];
            selected = NO;
        } else {
                [aTableview setHidden:YES];
                selected= YES;
        }
}

Upvotes: 0

Views: 161

Answers (5)

Anbu.Karthik
Anbu.Karthik

Reputation: 82766

declare the one Bool Value in your.h file, in this Bool Value default in 0

int selected;

.m file

-(void)ShowTable {

 switch (selected) {
    case 0:
       aTableview=[[UITableView alloc]initWithFrame:CGRectMake(20, 280, 128, 140)];
        aTableview.dataSource=self;
        aTableview.delegate=self;
        [scrollView addSubview:aTableview];


        selected=1;
        break;
    case 1:
         [aTableview setHidden:YES];
        selected=0;
        break;

    default:
        break;
}

   }

Upvotes: 0

dev gr
dev gr

Reputation: 2441

Make sure that IBAction touchUpInside is assigned to your button. I have modified your method. Take a look.

- (IBAction)toggleTableViewVisibility:(id)sender 
{
    selected = !selected;
    [self ShowTable];
}

-(void)ShowTable {
    if (selected)
   {
     //if tableView is already is a subview of scrollView don't recreate it.
     if([aTableView superView] == scrollView)  
      {         
        aTableview=[[UITableView alloc]initWithFrame:CGRectMake(20, 280, 128, 140)];
        aTableview.dataSource=self;
        aTableview.delegate=self;
        [scrollView addSubview:aTableview];
      }

        [aTableview setHidden:NO];
    } 
    else 
    {
        [aTableview setHidden:YES];    
    }
}

Upvotes: 0

Roma
Roma

Reputation: 1107

You can try change code a little bit

if (aTableView.hidden) {
     //prepare table
    [aTableView setHidden:NO];
} else {
    [aTableViewSetHidden:YES];
}

Upvotes: 0

Kirit Modi
Kirit Modi

Reputation: 23407

Add button in your view and give connection of it.

In your ViewController.h file

@property(nonatomic,retain)IBOutlet UIButton *btn;

and ViewController.m file add Tapgesture in your button

- (void)viewDidLoad
{

    recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];

    [recognizer setNumberOfTapsRequired:2];

    recognizer.cancelsTouchesInView = NO;

    [btn addGestureRecognizer:recognizer];

    [super viewDidLoad];

}

TapGesture Method.

- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
    NSLog(@" == Double Click");
}

Double click on your button and get log below:

enter image description here

Upvotes: 1

Viper
Viper

Reputation: 1408

Try this -

in your viewDidLoad method -

selected    =   YES;
aTableview=[[UITableView alloc]initWithFrame:CGRectMake(20, 280, 128, 140)];
aTableview.dataSource=self;
aTableview.delegate=self;
[scrollView addSubview:aTableview];
[aTableview setHidden:YES];

aTableview.layer.cornerRadius=7.0f;
aTableview.layer.borderWidth=0.5f;
aTableview.layer.borderColor=[[UIColor whiteColor]CGColor];

And change ShowTable as -

-(void)ShowTable {
    if (selected)
    {
        [aTableview setHidden:NO];
        selected = NO;
    } else {
        [aTableview setHidden:YES];
        selected= YES;
    }
}

as per the code tableview will be hidden initially and on button tap it will show. May this will solve your problem

Upvotes: 0

Related Questions