Iphone 如何将数据从SQLite传递到UITextVIew?
我试图从数据库中检索数据并在UITextView中显示出来,但当我运行项目时,TextView将为空 AppDelegate.m:Iphone 如何将数据从SQLite传递到UITextVIew?,iphone,objective-c,uitextview,xcode4.3,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 i
-(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);
}
图h:
@interface Plot : NSObject {
NSString *description;
}
@property (nonatomic,retain) NSString *description;
-(id)description:(NSString *)d;
@end
图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];
}
在Appdalegate方法中将NSMutablearray作为返回:
-(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];
}
天哪,你对init方法做了什么?我相信我们会在一个大灌木丛下找到irs corps…我在“NSMutableArray*temp=[AppDelegate readPlotsFromDatabase]”行上看到一个错误,说“没有可见的@interface for'AppDelegate'声明选择器'readPlotsFromDatabase'”;“我编辑了NSMutableArray部分,现在我已经能够从数据库中检索数据了。谢谢。在AppDelegate.h文件中添加mathod声明。