2016-04-04 2 views
0

На самом деле Этот вопрос задан так много раз. Но у меня есть путаница среди них. Я пробовал это.Установить локальное уведомление при выборе ячейки таблицы

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([[cell.btn backgroundImageForState:UIControlStateNormal]isEqual:[UIImage imageNamed:@"unchecked.png"]]) 
    { 
     [cell.btn setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; 

    UILocalNotification *reminderNote =[[UILocalNotification alloc]init]; 
    reminderNote.soundName = @"music.mp3"; 
    [[UIApplication sharedApplication] scheduleLocalNotification:reminderNote]; 
    reminderNote.alertBody = @"Wish birthday to :%@",[kAppDelegate.commString objectAtIndex:indexPath.row]; 

    NSString *date =[kAppDelegate.String objectAtIndex:indexPath.row]; 

    NSDate *dateP = [ dateformat dateFromString:date]; 
    components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:dateP]; 
     [components setHour:4]; 
     [components setMinute:59]; 
     [components setSecond:10]; 

    reminderNote.fireDate = [[NSCalendar currentCalendar] dateFromComponents:components]; 
    } 

Где клетка является объектом UITableviewCell, но Существует не любой тип уведомления. Я знаю, что есть небольшая ошибка, пожалуйста, помогите мне узнать.

ответ

2
  • Место последней строки, как [[UIApplication sharedApplication] scheduleLocalNotification: reminderNote]; после reminderNote.fireDate линия.

  • зарегистрировать reminderNote.fireDate и проверить формат.

Надеюсь, это поможет.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     if([[cell.btn backgroundImageForState:UIControlStateNormal]isEqual:[UIImage imageNamed:@"unchecked.png"]]) 
     { 
      [cell.btn setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; 

     UILocalNotification *reminderNote =[[UILocalNotification alloc]init]; 
     reminderNote.soundName = @"music.mp3"; 
     reminderNote.alertBody = @"Wish birthday to :%@",[kAppDelegate.commString objectAtIndex:indexPath.row]; 

     NSString *date =[kAppDelegate.String objectAtIndex:indexPath.row]; 

     NSDate *dateP = [ dateformat dateFromString:date]; 
     components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:dateP]; 
      [components setHour:4]; 
      [components setMinute:59]; 
      [components setSecond:10]; 

     reminderNote.fireDate = [[NSCalendar currentCalendar] dateFromComponents:components]; 
     [[UIApplication sharedApplication] scheduleLocalNotification:reminderNote]; 
     } 
0

Вы можете добавить локальные уведомления upto 64 за один раз.

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
    NSDate *newDate = [fireDate dateByAddingTimeInterval:(6*60*60)]; 
    // 6*60*60=6 hour*60 minutes*60 seconds 
    // [fireDate dateByAddingTimeInterval:(6*60*60*i)]; 
    localNotification.fireDate = newDate; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotification.repeatInterval = 0; 
    localNotification.alertBody = alertText; 
    localNotification.alertAction = alertAction; 
    if(soundfileName == nil) 
    { 
     localNotification.soundName = UILocalNotificationDefaultSoundName; 
    } 
    else 
    { 
     localNotification.soundName = soundfileName; 
    } 
    localNotification.alertLaunchImage = launchImage; 
    localNotification.applicationIconBadgeNumber = 1; 
    localNotification.userInfo = userInfo; 
    //  NSLog(@"%@", [localNotification description]); 
    // Schedule it with the app 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

Если вы хотите сохранить все объекты NSNotification, то вам нужно хранить что где-то. то есть в массиве.

Вы также можете использовать следующие методы для операции NSNotifications.

- (void)cancelLocalNotification:(UILocalNotification *)notification - (void)cancelAllLocalNotifications

Убедитесь, что вы получаете уведомления о

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

Как хорошая практика перед использованием объекта, убедитесь, что вы настроите и установить все значения на конкретный объект.

0

пожалуйста, попробуйте это для локального оповещения

-(void)localnotification{ 

    NSDate *pickerDate = [self.datepicker date]; 

    // Schedule the notification 
    UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
    localNotification.fireDate = pickerDate; 
    localNotification.alertBody = self.labelname.text; 
    localNotification.alertAction = @"Show me the item"; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
} 
Смежные вопросы