我有UIWebView
显示 HTML 页面。当在 webview 上长按时,我将 x 和 y 坐标保存到数据库。在UIMenuItem
中,单击笔记按钮,它会显示UITextView
用于写笔记。如果单击完成按钮,它将使用 x 和 y 坐标存储到数据库。然后Tag
为每个带有文本的可触摸坐标显示一个带有 x 和 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 坐标的数据库 textnote 表,然后在 x 和 y 坐标上显示 webviewbutton:
-(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];