UIButton标记获取上次更新值

时间:2013-11-08 04:28:38

标签: ios sqlite tags uibutton nsmutablearray

我需要在UIWebView中创建多个按钮。所以,我用标签按钮。然后我用INTEGER将按钮标签保存到数据库。但它没有保存适当的价值。每个按钮标签在数据库中都有文本。后来我需要检索。但是在将最后一个值存储到数据库并单击任何其他按钮后,它只获取最后更新的值。为什么会这样?

表:

textnotes          text_id   button_id

  hi                1         1
  how are you       1         3
im fine             2         7
  This is test       2         11

的UIButton:

int gTag = 1;
webviewbutton.tag = gTag;
 gTag++;

[wbCont.scrollView  addSubview:webviewbutton];

插入:

const char *sqlInsert = [[NSString stringWithFormat:@"insert into textnote (textnotes,text_id,button_id ) values ('%@','%@','%ld')", txtview.text,artID,(long)webviewbutton.tag] cStringUsingEncoding:NSUTF8StringEncoding];

使用数据库中的文本检索按钮标记:

    textArray=[[NSMutableArray alloc]init];

        webbutton_art=[[NSMutableArray alloc]init];

        webbutton_button=[[NSMutableArray alloc]init];

    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
    const char *sql =  [[NSString stringWithFormat:
                                     @"SELECT textnotes,text_id,button_id FROM textnote where button_id = '%ld' AND text_id = '%@' ",(long)webviewbutton.tag,artID]cStringUsingEncoding:NSUTF8StringEncoding];


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

                sqlite3_stmt *statement;

                if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

                    while (sqlite3_step(statement) == SQLITE_ROW) {

                        txtstring = [[NSString alloc] initWithUTF8String:
                                     (const char *) sqlite3_column_text(statement, 0)];


                        webbutton_textartid = sqlite3_column_int(statement, 1);

                        webbutton_buttontag= sqlite3_column_int(statement, 2);

          [textArray addObject:txtstring];

         [webbutton_art addObject:@(webbutton_textartid)];

                        [webbutton_button addObject:@(webbutton_buttontag)];


    if( webbutton_art  && webbutton_button ){

    for(int i=0;i<[textArray count];i++){


txtview = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,568)];

        txtview.font = [UIFont fontWithName:@"Helvetica" size:12];


             txtview.text=[textArray objectAtIndex:i];

 [self.view addSubview:txtview];

             }



    }

1 个答案:

答案 0 :(得分:0)

声明一个整数变量,如:

int offset;

然后这样做:

for (int i = 0; i<[textArray count]; i++) {



        UITextView *txtview = [[UITextView alloc]initWithFrame:CGRectMake(100,100+offset,320,568)];

        txtview.font = [UIFont fontWithName:@"Helvetica" size:12];


        txtview.text=[textArray objectAtIndex:i];

        [self.view addSubview:txtview];

        offset = offset + 50;
    }