如何在iphone中设置选定日期的闹钟?

Posted

技术标签:

【中文标题】如何在iphone中设置选定日期的闹钟?【英文标题】:How to set alarm for selected days in iphone? 【发布时间】:2012-05-28 06:53:18 【问题描述】:

我想为一周中的选定日期设置闹钟。

可能是工作日(周一、周二、周三、周四或周五)、周末(周六、周日)或每天。

如何使用本地通知来做到这一点?

【问题讨论】:

检查this 和Local Notification Tutorial。还要检查这个Apple Document。 【参考方案1】:

如果你有时间每天都必须触发通知,你应该这样做

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    for (NSMutableArray * arrDay in self.namaztimes) 
        NSLog(@"Alarm Array: %@", arrDay);
        int count=[arrDay count];
        if(!count)
            continue;
        

        int day =0;
        int month=0;
        int year=0;
        int hour =0;
        int minutes=0;

        //  NSArray *arrDates=[[NSMutableArray alloc] initWithCapacity:1];
        for ( int i=0;i<3;i++) 
            NSString * dayTime=[arrDay objectAtIndex:i ];
            if (i==0) 
                day = [dayTime intValue];    
            else if(i==1)
                month = [dayTime intValue];                    
            else if(i==2)
                year = [dayTime intValue];    

            
        
        for ( int i=3;i<count;i++) 
            NSString * dayTime=[arrDay objectAtIndex:i ];
            hour = [[dayTime substringToIndex:2] intValue];
            minutes = [[dayTime substringFromIndex:3] intValue];

            [components setDay:day];
            [components setMonth:month];
            [components setYear:year];
            [components setMinute:minutes];
            [components setHour:hour];


            NSDate *myNewDate = [calendar dateFromComponents:components];

            [self scheduleNotificationForDate:myNewDate];

        
    

    [components release];
    [calendar release];

然后从这里连接到主通知触发方法[self scheduleNotificationForDate:myNewDate];

-(void) scheduleNotificationForDate: (NSDate*)date 
    /* Here we cancel all previously scheduled notifications */
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = date;
    NSLog(@"Notification will be shown on: %@ ",localNotification.fireDate);

    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = @"Your Notification Text"; //[NSString stringWithFormat:@"%@",date];
    localNotification.alertAction = NSLocalizedString(@"View details", nil);

    /* Here we set notification sound and badge on the app's icon "-1" 
     means that number indicator on the badge will be decreased by one 
     - so there will be no badge on the icon */

    localNotification.repeatInterval = NSDayCalendarUnit;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = -1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

【讨论】:

嗨,sree charan,谢谢,我只想为选定的日子设置闹钟。我该怎么做? 自定义我的代码,我已经为所有日历事件,日,月,年,小时等提供了 int 值,只需将您的值动态发送到此方法 这意味着,是否可以从数组中给出日期值以及 self.namaztimes 是什么?(这是闹钟时间。) 是的,您可以这样做,自定义方法并向该方法发送参数。 这是一个数组,其中我有要被解雇的日期,我的意思是完整的日期格式和时间【参考方案2】:

我知道这个问题是很久以前提出的,我添加这个是因为它会对某些人有所帮助。

如果您想在选定日期通知,请关注此tutorial。会有帮助的。

【讨论】:

以上是关于如何在iphone中设置选定日期的闹钟?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 multiDatesPicker 中设置选定的日期

如何在 iOS 中设置闹钟?

在代码中设置 iPhone 闹钟?

如何以编程方式设置 iPhone 闹钟?

如何在 Flutter 中设置闹钟?

如何在数据表中设置选定列排序?