I am doing an alarm app for iOS, but I am a bit confused in taking data from different classes and save them to db in controller class.
for taking name, I have Name class, for taking time I have Time Class, for taking ringtone type, I have Ringtone class, so I am taking different values for one alarmTable(sqlite table) and saving them to db on save button which is in Controller.
I thought to take from every class and save them to delegate variables, and then fetch in controller class, is it almost successful, but having trouble in saving again default values,
Can anyone guide me that what is logic behind this?
These are variables in appDelegate
NSString *name;
NSString *time;
NSString *repeat;开发者_运维技巧
NSString *sound;
NSString *snooz;
NSString *soundFade;
NSString *volume;
NSString *vibrate;
NSString *soundName;
This is way of getting values from appDelegates
-(NSString *) getName {
return name;
}
-(NSString *) getTime {
return time;
}
-(NSString *) getRepeat {
return repeat;
}
-(NSString *) getSound {
return sound;
}
-(NSString *) getSnooz {
return snooz;
}
-(NSString *) getSoundFade {
return soundFade;
}
-(NSString *) getVolume {
return volume;
}
-(NSString *) getVibrate {
return vibrate;
}
and when I do assign values to these delegate variables, I do write in different classes are below
AlarmProjectAppDelegate *delegate = (AlarmProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate setName:name_textField.text];//name_textField contain alarmname
and I do like this before adding to Database
AlarmProjectAppDelegate *delegate = (AlarmProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
//data for databas for new alarm start
NSString *name=[delegate getName];
NSString *time=[delegate getTime];
NSString *repeat=[delegate getRepeat];
NSString *sound=[delegate getSound];
NSString *snooz=[delegate getSnooz];
NSString *soundFade=delegate.soundFade;
NSString *volume=[delegate getVolume];
NSString *vibrate= [delegate getVibrate];
To make it simpler, you have to do is...
1. Create a separate class for individual table and declare all columns of table as variable in it.
2. Whenever you want to insert into any table, just create an instance of that table class and set all the variables.
3.Finally, pass that instance to your insert query as a parameter and get values from that.
For example...Create a class for Alarm table as below.
For master class, I'm modifying partial code. Assume it with name, time and repeat class. In Alarm.h
#import <Foundation/Foundation.h>
@interface Alarm : NSObject {
name *objName;
time *objTime;
repeat *objRepeat;
}
// sample code for master class.
@property (nonatomic, retain) NSString *objName;
@property (nonatomic, retain) NSString *objTme;
@property (nonatomic, retain) NSString *objRepeat;
@end
In Alarm.m
#import "Alarm.h"
@implementation Alarm
@synthesize objName;
@synthesize objTime;
@synthesize objRepeat;
@end
Now, call your insert query as given below...
- (void) InsertAlarmData : (Alarm*)objAlarm
{
name *objName = objAlarm.objName;
time *objTime = objAlarm.objTime;
repeat *objRepeat = objAlarm.objRepeat;
NSString *query = [NSString stringWithFormat:@"INSERT into Alarm(name, time, repeat) values ('%@','%@','%@')", objAlarm.objName, objAlarm.objTime, objAlarm.objRepeat];
const char *sql = [query cStringUsingEncoding:NSUTF8StringEncoding];
if (sqlite3_open([databasePath UTF8String], &dbaseConnection) == SQLITE_OK)
{
if (sqlite3_prepare_v2(masterDBase, sql, -1, &hydrate_statement, NULL) == SQLITE_OK)
{
int success = sqlite3_step(hydrate_statement);
sqlite3_reset(hydrate_statement);
if (success != SQLITE_DONE)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Database operation is not successful.." message:@" " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
NSLog(@"Error: failed to excecute query domain with message '%s'.", sqlite3_errmsg(masterDB));
}
}
else
{
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg (masterDB));
}
sqlite3_reset(hydrate_statement);
sqlite3_finalize(hydrate_statement);
}
}
To set values in Alarm class...
name *objName = [[name alloc] init];
objName.name = @"ABC";
time *objTime = [[time alloc] init];
objTime.time = @"5 AM";
repeat *objRepeat = [[repeat alloc] init];
objRepeat.repeat = @"After 5 mins";
Alarm *objAlarm = [[Alarm alloc] init];
objAlarm.objName = objName;
objAlarm.objTime = objTime;
objAlarm.objRepeat = objRepeat;
[self InsertAlarmData:objAlarm];
Hope, you are getting my point exactly now.
精彩评论