[__NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0]而获取textvalue

时间:2013-11-05 10:46:13

标签: ios tags uibutton uitextview

我有UIWebView用于显示HTML页面。当webview上的longpress我正在获得x& y坐标保存到数据库。在UIMenuItem中,单击“备注”按钮,它会显示UITextView以便写入备注。如果单击完成按钮,它将存储到数据库x& y坐标。然后在x&上显示Tag的一个按钮。每个可触摸坐标与文本的y坐标。稍后,如果我单击按钮,标签将与我需要显示数据库中的文本的坐标匹配。但我得到错误

-(void)viewDidLoad{

NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];
    if (!items) items = [[NSMutableArray alloc] init];

    UIMenuItem *menuItem;
    menuItem = [[UIMenuItem alloc] initWithTitle:@"BookMark" action:@selector(book:)];
    [items addObject:menuItem];
    [menuItem release];
    menuItem = [[UIMenuItem alloc] initWithTitle:@"Notes" action:@selector(note:)];
    [items addObject:menuItem];
    [menuItem release];

    [[UIMenuController sharedMenuController] setMenuItems:items];


    [items release];


    tap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapTest:)];
    [tap setDelegate:self];


    [wbCont.scrollView addGestureRecognizer:tap];

}

长按坐标保存:

- (void)tapTest:(UILongPressGestureRecognizer *)sender {
    NSLog(@"coordinate is %f %f", [sender locationInView:wbCont.scrollView].x, [sender locationInView:wbCont.scrollView].y);


     xcor = [sender locationInView:wbCont.scrollView].x;
     ycor = [sender locationInView:wbCont.scrollView].y;


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];


    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];
    NSLog(@"filepath %@",path);


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



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


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

        BOOL favExist = false;

        sqlite3_stmt *statement, *addStmt;

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



                favExist = true;
            }
        }


        if(!favExist){



            const char *sqlInsert = [[NSString stringWithFormat:@"insert into touch (xcoor,ycoor,artt_id) values ('%f','%f','%@')", xcor,ycor,artID] cStringUsingEncoding:NSUTF8StringEncoding];

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


            // [catID release];

            if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) != SQLITE_OK)
                NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

            NSLog(@"error is %s",sqlite3_errmsg(database));

            if(SQLITE_DONE != sqlite3_step(addStmt))
                NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));


        }else {


        }

    }

}

单击UIMenuItem上的注释:

- (void)note:(id)sender   {

    NSLog(@"Note");


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

    txtview.font = [UIFont fontWithName:@"Helvetica" size:12];
    txtview.font = [UIFont boldSystemFontOfSize:12];
    txtview.backgroundColor = [UIColor whiteColor];
    txtview.scrollEnabled = YES;
    txtview.pagingEnabled = YES;
    txtview.editable = YES;
     txtview.tag = mainTag*10000;




    [self.view addSubview:txtview];


 button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(done:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Done" forState:UIControlStateNormal];
    button.frame = CGRectMake(240.0, 20.0, 60.0, 40.0);
    [txtview addSubview:button];



}
在UITextView中写入文本后,单击“完成”按钮保存到数据库文本表格x& y坐标然后在x&上显示webview按钮。 y坐标:

-(void)done:(id)sender  {


    NSLog(@"done");


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // NSString *documentsDirectory = [paths objectAtIndex:1];

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


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



        const char *sql = [[NSString stringWithFormat:@"SELECT textnotes,text_id,txcoor,tycoor FROM textnote where textnotes = '%@' AND text_id = '%@' AND txcoor = '%f' AND tycoor = '%f'",txtview.text,artID,xcor,ycor] cStringUsingEncoding:NSUTF8StringEncoding];

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

        BOOL favExist = false;

        sqlite3_stmt *statement, *addStmt;

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


                favExist = true;
            }
        }


        if(!favExist){


            const char *sqlInsert = [[NSString stringWithFormat:@"insert into textnote (textnotes,text_id,txcoor,tycoor ) values ('%@','%@','%f','%f')", txtview.text,artID,xcor,ycor] cStringUsingEncoding:NSUTF8StringEncoding];

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

            // [catID release];

            if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) != SQLITE_OK)
                NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

            if(SQLITE_DONE != sqlite3_step(addStmt))
                NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));



        }else {



        }

    }



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



                table_touch_xcor=sqlite3_column_double(statement, 0);

                table_touch_ycor=sqlite3_column_double(statement, 1);

                table_touch_artid = sqlite3_column_int(statement, 2);

                if(table_touch_xcor && table_touch_ycor && table_touch_artid){



                    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 textnotes,text_id,txcoor,tycoor FROM textnote where  text_id = '%@' AND txcoor = '%f' AND tycoor = '%f'",artID,table_touch_xcor,table_touch_ycor] 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) {




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


                                table_textnote_artid = sqlite3_column_int(statement, 1);


                                table_textnote_xcor=sqlite3_column_double(statement, 2);

                                table_textnote_ycor=sqlite3_column_double(statement, 3);



                                if(table_textnote_xcor && table_textnote_ycor && table_textnote_artid ){


                                    webviewbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

                                    [webviewbutton addTarget:self action:@selector(webviewbuttonclick1:) forControlEvents:UIControlEventTouchUpInside];

                                    [webviewbutton setTitle:@"click" forState:UIControlStateNormal];

                                    webviewbutton.frame = CGRectMake(table_textnote_xcor, table_textnote_ycor, 30.0, 20.0);


                                    webviewbutton.tag = gTag;
                                    gTag++;

                                    [wbCont.scrollView  addSubview:webviewbutton];






                                }




                            }
                        }
                    }


                }



            }
        }
    }

然后webviewbutton点击我正在获取x& y与数据库中保存的文本坐标。在这里,我需要获取webviewbutton标签的保存文本。

-(void)webviewbuttonclick1:(id)sender{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // NSString *documentsDirectory = [paths objectAtIndex:1];

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];



    textArray=[[NSMutableArray alloc]init];


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


      //  const char *sql =  [[NSString stringWithFormat:
                           //  @"SELECT textnotes,text_id,txcoor,tycoor FROM textnote where txcoor = '%f' AND tycoor = '%f'",xcor,ycor]cStringUsingEncoding:NSUTF8StringEncoding];





        const char *sql ="SELECT textnotes,text_id,txcoor,tycoor FROM textnote where txcoor = 113.0 AND tycoor = 342.0";

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


                art_Idt = sqlite3_column_int(statement, 1);

                tx = sqlite3_column_double(statement, 2);
                ty = sqlite3_column_double(statement, 3);

                NSLog(@"txtstring is %@",txtstring);

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

                NSLog(@"tx is %f",tx);
                NSLog(@"ty is %f",ty);

                [textArray addObject:txtstring];

                NSLog(@"textArray is %@",textArray);





            }
        }


    }


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

    txtt.text=txtstring;

    NSLog(@"text is %@",txtt.text);

    [self.view addSubview:txtt];

1 个答案:

答案 0 :(得分:5)

当您访问大于数组大小的元素时,通常会显示错误消息[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]

在这种情况下,您的数组只有一个元素,因此如果要访问该元素,则需要使用objectAtIndex:0。既然你没有告诉我们你如何初始化/填充数组,我不能帮助你。

对于所有圣洁的爱,请尝试使用更好的标识符名称并缩进代码。在这个世界上没有人,除了你之外,没有人会理解click1button1button4的含义。事实上,在两周内,我可以向你保证,你会忘记click1应该是什么意思。相信我,如果你继续这样做,你会很快变得讨厌编程。