我需要将数据从一个视图传递到另一个视图并在第二个视图上检索该数据

时间:2015-09-16 08:25:51

标签: objective-c

第一个视图是单词列表视图控制器,它有一个包含word_id,单词含义和句子的单词表,这个数据我需要传递给另一个有1个文本字段的视图,我添加它来显示单词和两个文本视图显示意义和句子。 这是代码 -

- (void)getListData 
{
//tableData = [[NSMutableArray alloc]init];
sqlite3_stmt *statement;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dexternotedb.sqlite"];

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
    WordListViewController *temp = APP_DELEGATE.wordListViewController;
    NSLog(@"word id %ld",(long)[temp.wordId integerValue]);

    NSString *sql=[NSString stringWithFormat:@"SELECT word_id, word ,meaning,sentence FROM words WHERE word_id=\"%@\"",APP_DELEGATE.wordListViewController.wordId];
    const char *read_stmt = [sql UTF8String];
    NSLog(@"select query%@",sql);


    if(sqlite3_prepare_v2(database, read_stmt, -1, &statement, NULL) == SQLITE_OK){
        //sqlite3_bind_int(statement, 1, [temp.wordId integerValue]);

        while(sqlite3_step(statement) == SQLITE_ROW) {
            NSString *word1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
            word.text = word1;
            NSString *meaning1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
            meaning.text = meaning1;

            NSString *sentence1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
            Sentence.text = sentence1;



        }


        sqlite3_finalize(statement);
    } else 
    {
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
    }
}

1 个答案:

答案 0 :(得分:2)

只需在NSUserDefaults中存储值

 [[NSUserDefaults standardUserDefaults] setObject:@"String" forKey:@"KeyName"];
 [[NSUserDefaults standardUserDefaults] synchronize];

并且每当需要数据时,从整个应用程序中的NSUserDefaults获取数据

  NSString *str= [[NSUserDefaults standardUserDefaults] valueForKey:@"KeyName"];