populating db records in a common NIB(Because same UI but different data from sqlite ) depending upon button clickd
i have followed this tutorial i am working on a simple game project.I am having a view consisting of uibutton.for eg, Movies Button, Places Button and Personalities Button. Now on clicking on Movies button a new NIB opens.Similarly for clicking on Places and for Personalities buttons the same NIB opens.
Note:NIB is same for all three buttons Movies,Places and Personalities,but data to be populated on that NIB is different.
i have done some stuff regarding connecting to database through resources and adding framework.Holding info(for Movies,Places and Personalities) in corresponding arrays in appdelegate. I have used NSObject class(Questions.h and .m) having id and name for question Movies,Places and Personalities.I had initialize them.
Problem:i want to open a single nib class/file having different instance(same UI but diffenet data depending on type of button pressed Movies,Places and Personalities).Some of the code snippts for my approach:
my appdelegate.h
UIWindow *window;
UINavigationController *navigationController;
NSString *databaseName;
NSString *databasePath;
sqlite3 *database;
// Array to store the Movies objects
NSMutableArray *movies;
// Array to store the places objects
NSMutableArray *places;
// Array to store the personalities objects
NSMutableArray *personalities; }
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *movies;
@property (nonatomic, retain) NSMutableArray *places;
@property (nonatomic, retain) NSMutableArray *personalities;
My appdelegate.m
-(void) readMoviesFromDatabase
{
const char *sqlStatement = "select * from tblMovies";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSString *amovieName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSInteger amovieId = sqlite3_column_int(compiledStatement, 2);
Questions *questions = [[Questions alloc] initWithmovieName:amovieName movieId:amovieId];
[movies addObject:questions];
[questions release];
}
sqlite3_finalize(compiledStatement);
}
[self closeDataBase];
}
Similarly for:
-(void) readPlacesFromDatabase and (void) readPersonalitiesFromDatabase
My Question.m file:
-(id)initWithmovieName:(NSString *)mN movieId:(NSInteger)mId
{
self.movieName=mN;
self.movieId=mId;
return sel开发者_高级运维f;
}
**similarly for PlaceName and PersonalityName.**
My GameCategory.m
**//I am stuffing here what do here so that on clicking any of the button the same NIB //opens but with different data.**
-(IBAction) moviesButtonPressed:(id)sender
{
TheCommonNib *theCommonNib = [[TheCommonNib alloc] initWithNibName:@"TheCommonNib" bundle:nil];
[self.navigationController pushViewController:theCommonNib animated:YES];
[theCommonNib release];
}
-(IBAction) placesButtonPressed:(id)sender
{
TheCommonNib *theCommonNib = [[TheCommonNib alloc] initWithNibName:@"TheCommonNib" bundle:nil];
[self.navigationController pushViewController:theCommonNib animated:YES];
[theCommonNib release];
}
Please help or guide me.
initialize one flag variable in appdelegate.h and didfinish method in appdelegate.m..and change that value in button pressed event (if button1 pressed,flag value will be 1 or button2 pressed,flag value will be 2,and compare that value in Nib view controller viewWillAppear method,for example through if condition...
精彩评论