0

我正在尝试从数据库中检索数据并将其显示在 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);

}

绘图.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];
}
4

1 回答 1

0

将 NSMutablearray 作为 Appdalegate 方法中的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];
}
于 2012-07-25T06:08:41.627 回答