按钮不显示来自viewcontroller的开关

时间:2013-10-29 07:50:40

标签: ios uiwebview sqlite uibutton

我从UIWebView获取触摸坐标并存储到数据库。后来我从数据库中获取这些值,如果这些值匹配,我在该坐标上创建一个按钮。我使用标签创建了多个按钮,但是从UIWebView返回并转到UIWebView时,最后一个坐标按钮仅显示所有其他按钮未显示。为什么呢?

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) {



                xff=sqlite3_column_double(statement, 0);

                yff=sqlite3_column_double(statement, 1);

                art_Id = sqlite3_column_int(statement, 2);

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

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

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

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


                    if(xff && yff && art_Id){

        NSLog(@"error");

        btnArray=[[NSMutableArray alloc]init];

        NSLog(@"xff is %f",xff);
        NSLog(@"yff is %f",yff);
        NSLog(@"art_idd is %ld",(long)art_Id);

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


        button1.tag = gTag;
        gTag++;

        [wbCont.scrollView  addSubview:button1];



    }




            }
        }
    }

我为viewDidLoad使用了相同的代码;

-(void)viewDidLoad{

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) {



                xff=sqlite3_column_double(statement, 0);

                yff=sqlite3_column_double(statement, 1);

                art_Id = sqlite3_column_int(statement, 2);

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

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

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

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



    if(xff && yff && art_Id){

        NSLog(@"error");


        NSLog(@"xff is %f",xff);
        NSLog(@"yff is %f",yff);
        NSLog(@"art_idd is %ld",(long)art_Id);

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


        button1.tag = gTag;
        gTag++;

        [wbCont.scrollView  addSubview:button1];

        }




            }
        }
    }




}

1 个答案:

答案 0 :(得分:0)

我不明白帖子中代码的第一部分是什么。

无论如何,从你的viewDidLoad代码中,我假设您将获得多个按钮的坐标集(x,y),并且您想要在该位置创建按钮。

在您的代码中,您不是在任何循环中创建按钮,这意味着您只创建一个按钮。我的理解是,在你的while循环中,你只需要x和y坐标,你需要在这个循环中创建按钮。

尝试在while (sqlite3_step(statement) == SQLITE_ROW)循环

中添加此代码
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);
button1.tag = gTag;
gTag++;

[wbCont.scrollView  addSubview:button1];