【问题标题】:How to get which button clicking using Tag如何使用标签获取点击哪个按钮
【发布时间】:2013-11-16 01:58:32
【问题描述】:

我使用 tag++ 创建了多个按钮。我需要知道使用 sender 方法单击哪个按钮。单击带有标签的按钮时,我需要从数据库中获取值。我应该使用 if 方法作为点击按钮的标签位置

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

这里我需要知道按钮点击:

 -(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);


                }

            }

        }


}

【问题讨论】:

  • 看我的回答。你在方法中有按钮实例,你可以从那里做任何事情,首先类型转换然后只有你会得到正确的标签值
  • @staticVoidMan :更新了答案,之前的问题是这样的所以给出这样的答案。
  • 欢迎来到 Stack Overflow。请填写完整的问题,您可以编辑问题,但这不应该完全改变问题的上下文,因此答案看起来与所问的完全不同。有一些有疑问?寻找更多,然后只提出一个新问题。请阅读#FAQ
  • 根据当前编辑,switch case 不会执行,因为没有指定任何“case”。参考:techotopia.com/index.php/The_Objective-C_switch_Statement
  • 另外,在哪种情况下使用标签?即使(以某种方式)开关盒工作,所有标签都将执行相同的代码块。伙计们怎么了?

标签: ios tags uibutton


【解决方案1】:

发送者返回执行该操作的对象。首先将发送者类型转换为按钮,您可以拥有该对象可实现的所有属性

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

所以你的情况

 -(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);
    }
   }
 }
}

更新

是的,你可以使用它

注意

使用UIControlEventTouchUpInside实现按钮的点击事件

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

【讨论】:

  • 你能过来聊天吗?
【解决方案2】:

这样做很简单

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

【讨论】:

  • 我正在从数据库中获取一些值。你能检查我编辑的代码吗
猜你喜欢
  • 2014-06-21
  • 2020-09-03
  • 2011-08-08
  • 1970-01-01
  • 2021-10-28
  • 2011-11-22
相关资源
最近更新 更多