从选择表格单元格中设置本地通知

时间:2016-04-04 11:35:19

标签: ios uitableview localnotification

实际上这个问题已被问过这么多次。但我有困惑。我试过这个。

- (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的对象,但是没有任何类型的通知。我知道有一点Bug,请帮我查一查。

3 个答案:

答案 0 :(得分: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];
        }

答案 1 :(得分:0)

您可以一次添加最多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

作为使用对象之前的一个好习惯,请确保已初始化并将所有值设置为特定对象。

答案 2 :(得分: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];
}