如何在iPhone中设置闹钟并保存在本地通知中?

时间:2011-08-25 06:06:23

标签: iphone objective-c notifications

我正在iPhone中创建一个警报应用程序,它使用SQLite和本地通知。我有一个tableview类,其中包含4行用于选择时间,重复间隔,声音等。当我选择所有这些因素时,它们都存储在全局变量中。

在我的tableview中,我选择这些行,我有一个保存按钮。当我在选择特定行后单击保存按钮时,时间,重复和声音的值将通过此全局变量保存在SQLite数据库中。现在我将这些变量传递给我的通知对象,以便在单击“保存”按钮时设置警报。但通知没有设定。

这是我的通知代码:

//
//  TAddAlarmController.m
//  StopSnooze
//
//  Created by raji.nair on 7/18/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TAddAlarmController.h"
//#import "CustomCell.h"
#import "StopSnoozeAppDelegate.h"
#import "Alarm.h"
#import "TAlarmNewController.h"
#import "global.h"
#import "TTimePickerController.h"
#import "TAlarmSoundController.h"
#import "AlarmMessageController.h"
#import "TPenaltyController.h"
#import "TSnoozeIntervalController.h"

#define DATABASE_TITLE @"StopSnooze.sqlite"
#define DATABASE_NAME @"StopSnooze"

@implementation TAddAlarmController
@synthesize dateFormatter;
@synthesize am;
@synthesize undoManager;
@synthesize time;
@synthesize tblView;
@synthesize detailTxt;
//@synthesize controller;


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {


    [super viewDidLoad];
    self.title = @"Add Alarms";
    newtemp = anew.temp;
    UIBarButtonItem * saveButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
        self.navigationItem.rightBarButtonItem = saveButton;
        [saveButton release];

}


-(void)schedulealarm
{
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = (NSDate*)lblDate.text;
//lblDate.text is where my date is getting saved through the picker but in the fireDate it is showing null.
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    // Notification details
    localNotif.repeatInterval = (int)newrepeat;
//newrepeat is the global variable where is select the repeat interval for alarm.
    localNotif.alertBody = newmessage;
    // Set the action button
    localNotif.alertAction = @"View";

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;

    if (localNotif == nil) {
        NSLog(@"Yes");
    }
    else {
        NSLog(@"Notification called");
    }


    // Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];


}


- (void)viewDidUnload {
    // Release any properties that are loaded in viewDidLoad or can be recreated lazily.
    self.dateFormatter = nil;
}
-(void)saveInDatabase
{
    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication] delegate];
    [app copyDatabaseIfNeeded];
    NSString *filePath  =[app getDBPath];
    sqlite3 *database;

    NSTimeInterval timeStamp = [[NSDate date]timeIntervalSince1970];
    NSNumber *timeStampObj = [NSNumber numberWithInt:timeStamp];
    NSLog(@"%d",timeStampObj);

    AlarmID =(int)timeStampObj;
    NSLog(@"%@",AlarmID);

    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
    [formatter setDateFormat:@"HH:MM"];

    NSString *str = [formatter stringFromDate:date];
    NSLog(@"%@",str);

    if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = "insert into AlarmsInformation(alarm_id,alarm_time,snooze_interval,sound_file,alarm_message) VALUES (?,?,?,?,?)";
        sqlite3_stmt *compiledStatement;
        if (sqlite3_prepare_v2(database,sqlStatement ,-1, &compiledStatement,NULL) == SQLITE_OK) {
            //sqlite3_bind_text(compiledStatement, 1, [AlarmID UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_int(compiledStatement, 1, AlarmID);
            sqlite3_bind_text(compiledStatement, 2, [lblDate.text UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_int(compiledStatement, 3, databaseinterval);
            sqlite3_bind_text(compiledStatement,4 , [newsound UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 5, [newmessage UTF8String], -1, SQLITE_TRANSIENT);
        }
        if (sqlite3_step(compiledStatement)!= SQLITE_DONE) {
            NSLog(@"Save Error:%s",sqlite3_errmsg(database));
        }
        else {
            sqlite3_reset(compiledStatement);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Alarm Set" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
            alert = nil;
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);

}

-(IBAction)save{

    if (lblDate.text == NULL && interval == NULL && newsound == NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Set Date,interval,sound for alarm" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        alert = nil;
    }
    else {
        [self saveInDatabase];
        [self schedulealarm];
    }

    //[self.navigationController popViewControllerAnimated:YES];
}

@end

在appDelegate中,我为didReceiveNotification创建了一个方法。但通知不起作用。我的开火日期显示为空值。

可能是什么问题?我想将值传递给我存储在变量中的fireate。

1 个答案:

答案 0 :(得分:0)

我认为设置fireDate时会出现问题,你可以试试这个:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:day];
[components setMonth:month];
[components setYear:year];
[components setMinute:minutes];
[components setHour:hour];
NSDate *date = [calendar dateFromComponents:components];
localNotif.fireDate = date;
相关问题