1

iPhone の目覚まし時計アプリに似たピッカー ビューがあります (最初のコンポーネント = 1-12、2 番目のコンポーネント = 0-60、3 番目のコンポーネント = am/pm)。私がやっていることは、ピッカー ビューの didSelect デリゲートでこれです。

if (component == PICKER_HOURS) {
        rowSelected = [self.hour objectAtIndex:row % 12];
        self.hourSelected = rowSelected;
    }
    else if (component == PICKER_MINUTES) {
        rowSelected = [self.minutes objectAtIndex:row % 60];
        self.minuteSelected = rowSelected;
    }
    else {
        rowSelected = [self.ampm objectAtIndex:row];
        self.ampmSelected = rowSelected;
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    dateFormatter.dateFormat = @"h:mm a";

    NSString *timeString = [NSString stringWithFormat:@"%@:%@ %@", _hourSelected, _minuteSelected, _ampmSelected];
    NSString *trimmedTimeString = [timeString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    NSDate *dateSelected = [dateFormatter dateFromString:timeString];

    NSDate *dateForAlarm = [self alarmDateForDate:dateSelected];


------


- (NSDate *)alarmDateForDate:(NSDate *)dateSelected {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit)
                                                   fromDate:[NSDate date]];     // set the date to the current date
    NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:dateSelected];     // set the time to what's in the picker

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setHour:[timeComponents hour]];
    [dateComps setMinute:[timeComponents minute]];

    NSDate *dateForAlarm = [calendar dateFromComponents:dateComps];

    return dateForAlarm;
}

これは正しいアプローチですか?ピッカー ビューの dateFromString が常に 1970 時間であることに気付きました。したがって、これがピッカービューから時間を取得して通知を設定する「標準的な」方法であるかどうかはわかりませんでした。ありがとう!

4

1 に答える 1

0

これを見て -

日付ピッカー:-

m_pDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 50, 50)];
    m_pDatePicker.datePickerMode = UIDatePickerModeDate;
    NSDate *minDate = [NSDate new];
    [m_pDatePicker setMinimumDate:minDate];

    m_pDatePicker.hidden = NO;
    [m_pClientView addSubview:m_pDatePicker];

    [m_pDatePicker addTarget:self action:@selector(pickDateLabel)forControlEvents:UIControlEventValueChanged];

    NSDateComponents* comps = [m_pDatePicker.calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit)
                                                        fromDate:m_pDatePicker.date];
    int myDay = [comps day];
    int myMonth = [comps month];
    int myYear = [comps year];

    pickDateLabel.text=[NSString stringWithFormat:@"%d/%d/%d",myMonth,myDay,myYear];

    dueDateLabel.text=[NSString stringWithFormat:@"%d/%d/%d",myDay,myMonth,myYear];

    [m_pDatePicker release];
    [minDate release];

}

ピッキング日 -

- (void)pickDateLabel
{

    NSDateComponents* comps = [m_pDatePicker.calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit)
                                                        fromDate:m_pDatePicker.date];
    int myDay = [comps day];
    int myMonth = [comps month];
    int myYear = [comps year];

    pickDateLabel.text=[NSString stringWithFormat:@"%d/%d/%d",myMonth,myDay,myYear];    

    dueDateLabel.text=[NSString stringWithFormat:@"%d/%d/%d",myDay,myMonth,myYear];


}

それが役に立てば幸い !!

于 2012-04-17T04:36:34.253 に答える