如何将数据从SQLite传递到UITextVIew?

时间:2012-07-25 05:53:07

标签: iphone objective-c uitextview xcode4.3

我正在尝试从数据库中检索数据并在UITextView中显示它,但是当我运行项目时,TextView将为空。

AppDelegate.m:

-(void) readPlotsFromDatabase {
// Setup the database object
sqlite3 *database;

// Init the Array
ps = [[NSMutableArray alloc] init];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select * from Plot";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

            // Create a new object with the data from the database
            Plot *plots = [[Plot alloc] description:aDescription];

            // Add the object to the Array
            [ps addObject:plots];

            // [themes release];
        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

}

Plot.h:

@interface Plot : NSObject {

NSString *description;

}

@property (nonatomic,retain) NSString *description;

-(id)description:(NSString *)d;

@end

Plot.m:

@implementation Plot

@synthesize description;

-(id)description:(NSString *)d {
self.description = d;
return self;
}

@end

PlotViewController.h:

@interface PlotViewController : UIViewController {

IBOutlet UITextView *plotDescription;

Plot *plots;
AppDelegate *appDelegate;
NSString *plotDes;

}

@property (nonatomic, retain) IBOutlet UITextView *plotDescription;

@property (nonatomic, retain) Plot *plots;
@property (nonatomic, retain) NSString *plotDes;

@end

PlotViewController.m:

- (void)viewDidLoad
{
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
plotDescription.text = plots.description;
[super viewDidLoad];
}

编辑:

我现在将PlotViewController.m更改为此功能,并且能够正常工作。

- (void)viewDidLoad
{
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    Plot *p = (Plot *)[appDelegate.ps objectAtIndex:0]; 
    plotDescription.text = p.description;
    [super viewDidLoad];
}

1 个答案:

答案 0 :(得分:0)

在Appdalegate方法中将NSMutablearray作为reture:

-(NSMutableArray) readPlotsFromDatabase {
// Setup the database object
sqlite3 *database;

// Init the Array
ps = [[NSMutableArray alloc] init];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select * from Plot";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];



            // Add the object to the Array
            [ps addObject:aDescription];


        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);


return ps;
}

还有PlotViewController.m:

- (void)viewDidLoad
{
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableArray *temp = [appDelegate readPlotsFromDatabase];
plotDescription.text = [temp objectAtindex:0];
[super viewDidLoad];
}