user2955041
user2955041

Reputation: 13

How to get which button clicking using Tag

I created more than one button using tag++. I need to know which button clicking with sender method. When clicking button with tag i need to get values from database. shall i use if method for tag position for click button

button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 addTarget:self action:@selector(click1:)                                    forControlEvents:UIControlEventTouchDown];
     [button1 setTitle:@"click" forState:UIControlStateNormal];

     button1.frame = CGRectMake(xff, yff, 30.0, 20.0);

    int gTag = 1;                                    

      button1.tag = gTag;
       gTag++;

    [wbCont.scrollView  addSubview:button1];

Here i need to know button click:

 -(void)click1:(id)sender{


UIButton *button3=(UIButton *)sender;
switch([button3 tag]){
        //Do the switch case here

        NSArray *coorpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *coordocumentsDirectory = [coorpaths objectAtIndex:0];
        NSLog(@"docs dir is %@", coordocumentsDirectory);

        NSString *coorpath = [coordocumentsDirectory stringByAppendingPathComponent:@"ohs.sqlite"];
        NSLog(@"filepath %@",coorpath);

        if (sqlite3_open([coorpath UTF8String], &database) == SQLITE_OK) {


            const char *sql =  [[NSString stringWithFormat:
                                 @"SELECT xcoor,ycoor,artt_id FROM touch where artt_id = %@", artID]cStringUsingEncoding:NSUTF8StringEncoding];


            NSLog(@"getmainsql is %s",sql);

            sqlite3_stmt *statement;

            if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
                // We "step" through the results - once for each row.
                while (sqlite3_step(statement) == SQLITE_ROW) {



                    xff1=sqlite3_column_double(statement, 0);

                    yff1=sqlite3_column_double(statement, 1);

                    art_Id2 = sqlite3_column_int(statement, 2);

                    NSLog(@"xff1 is %f",xff1);

                    NSLog(@"yff1 is %f",yff1);

                    //  NSLog(@"zc is %@",zc);

                    NSLog(@"art_Id is %ld",(long)art_Id2);


                }

            }

        }


}

Upvotes: 1

Views: 8355

Answers (2)

Lithu T.V
Lithu T.V

Reputation: 20021

Sender returnes the object which performed the action.Typecast first the sender to button ,You can have all the properties achievable from tht object

-(void)click1:(id)sender{
    UIButton *button=(UIButton *)sender;
    switch([button tag]){
    //Do the switch case here
    }
}

So in your case

 -(void)click1:(id)sender{

        UIButton *button=(UIButton *)sender;
NSInteger tag = [button tag];
if( tag){
 NSArray *coorpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *coordocumentsDirectory = [coorpaths objectAtIndex:0];
    NSLog(@"docs dir is %@", coordocumentsDirectory);
    NSString *coorpath = [coordocumentsDirectory stringByAppendingPathComponent:@"ohs.sqlite"];
    NSLog(@"filepath %@",coorpath);
    if (sqlite3_open([coorpath UTF8String], &database) == SQLITE_OK) {
        const char *sql =  [[NSString stringWithFormat:
                             @"SELECT xcoor,ycoor,artt_id FROM touch where artt_id = %@", artID]cStringUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"getmainsql is %s",sql);
        sqlite3_stmt *statement;
        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
            // We "step" through the results - once for each row.
            while (sqlite3_step(statement) == SQLITE_ROW) {
                xff1=sqlite3_column_double(statement, 0);
                yff1=sqlite3_column_double(statement, 1);
                art_Id2 = sqlite3_column_int(statement, 2);
                NSLog(@"xff1 is %f",xff1);
                NSLog(@"yff1 is %f",yff1);
                //  NSLog(@"zc is %@",zc);
                NSLog(@"art_Id is %ld",(long)art_Id2);
    }
   }
 }
}

UPDATE

Yes you can use it

Note

use UIControlEventTouchUpInside for achieving click event of button

[button addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 3

Niko Adrianus Yuwono
Niko Adrianus Yuwono

Reputation: 11112

it's easy just do this

-(void)click1:(id)sender{
    int tag = [sender tag];
    switch(tag){
    //Do the switch case here
    }
}

Upvotes: 2

Related Questions