gpt4 book ai didi

ios - 如何使用标签获取点击哪个按钮

转载 作者:行者123 更新时间:2023-12-01 17:54:46 25 4
gpt4 key购买 nike

我使用 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);


}

}

}


}

最佳答案

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

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

关于ios - 如何使用标签获取点击哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19784844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com