警告用户datePicker设置为今天

时间:2014-09-11 18:32:32

标签: objective-c uilocalnotification

我试图阻止用户设置当前日期或时间的通知,即。这篇文章的创建时间。需要关于代码的建议来制作UIAlert,当按下按钮创建警报时,他们被告知不可能。例如,我是否使用if语句表示如果按下按钮且时间是最小日期,则无法创建警报?这将是我的第一个应用程序。我只需要为明天搞这些错误。下面的代码用于安排警报,我会在那里的某处使用if语句:

- (IBAction)scheduleAlarm:(id)sender {

    [self.eventText resignFirstResponder];

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

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

    //Unable to set notification for same day
    [datePicker setMinimumDate:[NSDate date]];

    //Break the date up into components
    NSDateComponents *dateComponents = [calendar components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components: (NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:pickerDate];

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

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

    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil];
    localNotification.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    // Request to reload table view data
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

    eventText.text = nil;

    [self dismissViewControllerAnimated:YES completion:nil];

}

1 个答案:

答案 0 :(得分:0)

在datePicker行之后使用以下:

NSDate *datePicker = [self.datePicker date];
NSDate *dateNow = [NSDate date];
if ([dateNow compare:datePicker] == NSOrderedAscending) {

 // show alert and return

}