2011-11-04 57 views
0

: -UILocalNotification是不調用編輯時間正確的時間

對不起,重新編輯的問題,但其實我沒有得到答案,我需要什麼。我認爲我問這個問題的方式是錯誤的,所以重新編寫整個問題:

我已經制作了應用程序,並在其中定製了一個警報應用程序。 UILocalNotification應該在我在DatePicker中選擇的時間調用,但不是在正確的時間調用。 例如,我爲鬧鐘選擇時間爲下午2:00,所以通知將在下午2:00到2:01 PM之間進行調用...但不確定何時...它給我一個隨機時間的延遲。 在我的TableView中,你可以看到它是顯示的描述也是錯誤的。我知道我來自印度,所以它顯示了GMT時間,但是它可以被糾正?

這裏是馬全碼: -

----------------------------- AppDelegate.m文件: - -----------------------------

@synthesize window,viewController,timeViewController; 

NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey"; 

#pragma mark - 
#pragma mark === Application Delegate Methods === 
#pragma mark - 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  


    int x = [[NSUserDefaults standardUserDefaults] integerForKey:@"Mayank"]; 
    if(x == 1) 
    { 
     timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil]; 
     timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 

     CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; 
     CGRect frame = timeViewController.view.frame; 
     frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height); 
     timeViewController.view.frame = frame; 

     [self.window addSubview:timeViewController.view]; 
    } 
    else 
    { 
     [[NSUserDefaults standardUserDefaults]setInteger:1 forKey:@"Mayank"]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to set the Default Alarm?" message:@"at 4:20 PM" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil]; 
     [alert show]; 
     [alert release]; 
    } 

    sleep(1); 
    [self.window makeKeyAndVisible]; 

    application.applicationIconBadgeNumber = 0; 
    // Handle launching from a notification 
    UILocalNotification *localNotification = 
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    if (localNotification) { 
     NSString *reminderText = [localNotification.userInfo 
            objectForKey:kRemindMeNotificationDataKey]; 
     [viewController showReminder:reminderText]; 
    } 

    return YES; 
} 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex == 0) 
    { 
     timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil]; 
     timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 


     CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; 
     CGRect frame = timeViewController.view.frame; 
     frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height); 
     timeViewController.view.frame = frame; 


     [self.window addSubview:timeViewController.view]; 
    } 
    if(buttonIndex == 1) 
    { 
     viewController = [[SetAlarmViewController alloc]initWithNibName:@"SetAlarmViewController" bundle:nil]; 
     viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 

     CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; 
     CGRect frame = viewController.view.frame; 
     frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height); 
     viewController.view.frame = frame; 

     [self.window addSubview:viewController.view];  
    } 

} 

- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification { 

    NSString *reminderText = [notification.userInfo 
           objectForKey:kRemindMeNotificationDataKey]; 
    [viewController showReminder:reminderText]; 
    application.applicationIconBadgeNumber = 0; 
} 

------------ ----------------- mainViewController.m文件: - ------------------------- ----

@implementation SetAlarmViewController 
@synthesize datePicker,tableview, eventText,titleBar,setAlarmButton,returnKeyType; 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

    [super viewDidLoad]; 

    appDelegate = (The420DudeAppDelegate *)[[UIApplication sharedApplication] delegate];  
    eventText.returnKeyType = UIReturnKeyDone; 

// datePicker.minimumDate = [NSDate date]; 
    NSDate *now = [NSDate date]; 
    [datePicker setDate:now animated:YES]; 
    eventText.delegate = self; 
    index = 0; 
} 

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:YES]; 
    [self.tableview reloadData]; 
} 

- (IBAction) scheduleAlarm:(id) sender { 
    [eventText resignFirstResponder]; 

// Get the current date 
    NSDate *pickerDate = [self.datePicker date]; 

    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    if (localNotif == nil) 
     return; 
    localNotif.fireDate = pickerDate; 
// NSLog(@"%@",localNotif.fireDate); 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
// NSLog(@"%@",localNotif.timeZone); 

    // Notification details 
    localNotif.alertBody = [eventText text]; 

    // Set the action button 
    localNotif.alertAction = @"Show me"; 
    localNotif.repeatInterval = NSDayCalendarUnit; 
    localNotif.soundName = @"jet.wav"; 
    // Specify custom data for the notification 
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:eventText.text 
                 forKey:kRemindMeNotificationDataKey]; 
    localNotif.userInfo = userDict; 

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

    [self.tableview reloadData]; 
    eventText.text = @""; 

    viewController = [[TimeViewController alloc] initWithNibName:@"TimeViewController" bundle:nil]; 
    [self presentModalViewController:viewController animated:YES]; 
} 

#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [[[UIApplication sharedApplication] scheduledLocalNotifications] count]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    index = indexPath.row; 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning!!!" 
                 message:@"Are you sure you want to Delete???" delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:@"Ok",nil]; 
    [alertView show]; 
    [alertView release]; 

} 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    UILocalNotification *notify = [notificationArray objectAtIndex:index]; 

    if(buttonIndex == 0) 
    { 
     // Do Nothing on Tapping Cancel... 
    } 
    if(buttonIndex ==1) 
    { 
     if(notify) 
      [[UIApplication sharedApplication] cancelLocalNotification:notify]; 
    } 
    [self.tableview reloadData]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    // Configure the cell... 

    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row]; 

    [cell.textLabel setText:notif.alertBody]; 
    [cell.detailTextLabel setText:[notif.fireDate description]];  
    return cell; 
} 


- (void)showReminder:(NSString *)text { 
    /* 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" 
    message:@"hello" delegate:self 
    cancelButtonTitle:@"OK" 
    otherButtonTitles:nil]; 
    [alertView show]; 
    [self.tableview reloadData]; 
    [alertView release]; 
    */ 

    NSString *path = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"]; 
    NSURL *url = [NSURL fileURLWithPath:path]; 

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; 
    player.numberOfLoops = -1; 
    [player play]; 

    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil]; 
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 
    [actionSheet showInView:self.view]; 
// [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]]; 
    [actionSheet release]; 

} 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    [player stop]; 
    if(buttonIndex == 0) 
    { 
     NSLog(@"OK Tapped"); 
    } 
    if(buttonIndex == 1) 
    { 
     NSLog(@"Cancel Tapped"); 
    } 
} 

This Pic S怎麼樣了我的應用程序查看: - Alarm App View

,此方法是調用enter image description here

回答

2

我認爲這裏的問題與NSDatePicker一起返回當前時間的秒數和選定的時間。您需要從NSDatePicker返回的日期中去除秒數,並將其用於鬧鐘和本地通知。

NSDate *selectedDate = [datePicker date]; // you don't need to alloc-init the variable first 
NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDateComponents *dc = [cal components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:selectedDate]; 
selectedDate = [cal dateFromComponents:dc]; // now you have an NSDate with zero seconds for your alarm 

你應該在通知上得到更好的準確性,但我認爲他們不會保證精確到秒。

1

這是一個問題:

NSDate *selectedDate = [[NSDate alloc] init]; 
    selectedDate = [datePicker date]; 

你泄露的NSDate你alloc和再扔掉。擺脫alloc/init並將nil指定給指針。但是這並不能解釋你的大問題。

除此之外,要麼是您沒有向我們展示的東西,要麼是您在採樣時仍然在移動日期選擇器。

+0

hehee ...我也讀過你以前的評論。 :) sry老兄,其實我已經寫了很多代碼,所以可能是我忘了糾正這一點。 – mAc

+0

還有什麼你需要....我應該粘貼我的整個代碼...它只是一個小項目只有一個視圖控制器....我只粘貼主要部分,因爲它不應該看到很長的問題,否則一些Genious投票它。 。:( – mAc

+0

我懷疑你沒有發佈整個scheduleAlarm方法,或者你發佈的消息與該版本的代碼不對應(對不起,看起來如此可疑,但我們總是看到這種事情。 ) –

1

如果您只需在NSDate對象上調用description(這就是您在cellForRowAtIndexPath方法中所做的操作),您將獲得日期的內部表示形式,格式爲GMT。您需要使用NSDateFormatter,並將其語言環境設置爲設備語言環境,並使用stringFromDate:方法獲取顯示日期。

+0

你可以給我一些代碼或鏈接或一些提示...如在我的代碼我重新加載表,所以它設置修改時間的所有行。:( – mAc