gpt4 book ai didi

iphone - 如何让 UIDatePicker 显示用户选择的时间

转载 作者:行者123 更新时间:2023-12-01 19:11:20 27 4
gpt4 key购买 nike

我有一个 UIDatepicker,让用户选择 time 。 UITableviewUIView点击一个 UIButton UIDdatePicker出现,当用户选择时间并按返回按钮关闭时 UIDdatePicker查看,如果他想查看他在 UIDdatePicker 中选择的时间是什么时候. UIDdatePicker不显示用户选择的时间它显示当前时间或 datepicker 的时间开始时间。 我想在用户再次打开日期选择器时显示用户选择的时间。
这是我在用户按下 UIButton 时的代码显示 UIDdatePicker看法

-(IBAction)settingsButtonChanged:(UIButton *)sender
{

userTimePicker = [[UIDatePicker alloc]init];
[userTimePicker addTarget: self
action: @selector(userTimePickerValueChanged:)forControlEvents: UIControlEventValueChanged];
}

这是我的代码,它让日期选择器具有自定义时间限制
-(IBAction)datepickerTimenButtonChanged:(UIButton *)control
{

if (control.tag == 1)
{
self.selectedButton = control.tag;

int startTime = 7;
int endTime = 11;

NSDate *date1 = [NSDate date];
components = [gregorian components: NSUIntegerMax fromDate: date1];

[components setHour:startTime];;
[components setMinute:00];

NSDate *startDate = [gregorian dateFromComponents:components];

[components setHour:endTime];
[components setMinute:00];

NSDate *endDate = [gregorian dateFromComponents:components];

morningButton.selected = YES;
afternoonButton.selected = NO;
eveningButton.selected = NO;
nightButton.selected = NO;

[userTimePicker setDatePickerMode:UIDatePickerModeTime];

[userTimePicker setMinimumDate:startDate];
[userTimePicker setMaximumDate:endDate];

[userTimePicker setDate:startDate animated:YES];
[userTimePicker reloadInputViews];

}

if (control.tag == 2)
{
self.selectedButton = control.tag;

morningButton.selected = NO;
afternoonButton.selected = YES;
eveningButton.selected = NO;
nightButton.selected = NO;

int startTime = 12;
int endTime = 15;

NSDate *date1 = [NSDate date];
components = [gregorian components: NSUIntegerMax fromDate: date1];

[components setHour:startTime];
[components setMinute:00];

NSDate *startDate = [gregorian dateFromComponents:components];

[components setHour:endTime];
[components setMinute:00];

NSDate *endDate = [gregorian dateFromComponents:components];

[userTimePicker setDatePickerMode:UIDatePickerModeTime];

[userTimePicker setMinimumDate:startDate];
[userTimePicker setMaximumDate:endDate];

[userTimePicker setDate:startDate animated:YES];
[userTimePicker reloadInputViews];

}
}

允许用户选择自己时间的代码
-(void) userTimePickerValueChanged:(UIButton *)sender
{

switch (self.selectedButton)
{
case 1:
{
NSUserDefaults *userSelectedMorningTime = [NSUserDefaults standardUserDefaults];

NSDate *selectedMorningTime = [userTimePicker date];

NSLog (@"SETTING: morningTimeKey");

storedMornTime = selectedMorningTime;

[userSelectedMorningTime setObject:selectedMorningTime forKey:@"morningTimeKey"];
[userSelectedMorningTime synchronize];
}
break;

case 2:
{
NSUserDefaults *userSelectedAfternoonTime = [NSUserDefaults standardUserDefaults];

NSDate *selectedAfternoonTime = [userTimePicker date];

NSLog (@"SETTING: afternoonTimeKey");

storedAfternoonTime = selectedAfternoonTime;

[userSelectedAfternoonTime setObject:selectedAfternoonTime forKey:@"afternoonTimeKey"];
[userSelectedAfternoonTime synchronize];
}
break;
}

最佳答案

从我从代码中可以看出,您将所选时间保存到 NSUserDefaults ,但您永远不会使用从中节省的时间。

我的猜测是,如果你改变一切都应该有效。

-(IBAction)datepickerTimenButtonChanged:(UIButton *)control
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (control.tag == 1)
{
self.selectedButton = control.tag;

int startTime = 7;
int endTime = 11;

//Saved Morning time date from user defaults
NSDate *date1 = [defaults objectForKey:@"morningTimeKey"];
if(!date1) date1 = [NSDate date];
components = [gregorian components: NSUIntegerMax fromDate: date1];

[components setHour:startTime];;
[components setMinute:00];

NSDate *startDate = [gregorian dateFromComponents:components];

[components setHour:endTime];
[components setMinute:00];

NSDate *endDate = [gregorian dateFromComponents:components];

morningButton.selected = YES;
afternoonButton.selected = NO;
eveningButton.selected = NO;
nightButton.selected = NO;

[userTimePicker setDatePickerMode:UIDatePickerModeTime];

[userTimePicker setMinimumDate:startDate];
[userTimePicker setMaximumDate:endDate];

[userTimePicker setDate:startDate animated:YES];
[userTimePicker reloadInputViews];

}

if (control.tag == 2)
{
self.selectedButton = control.tag;

morningButton.selected = NO;
afternoonButton.selected = YES;
eveningButton.selected = NO;
nightButton.selected = NO;

int startTime = 12;
int endTime = 15;

//Saved AfterNoon time date from user defaults
NSDate *date1 = [defaults objectForKey:@"afternoonTimeKey"];
if(!date1) date1 = [NSDate date];

components = [gregorian components: NSUIntegerMax fromDate: date1];

[components setHour:startTime];
[components setMinute:00];

NSDate *startDate = [gregorian dateFromComponents:components];

[components setHour:endTime];
[components setMinute:00];

NSDate *endDate = [gregorian dateFromComponents:components];

[userTimePicker setDatePickerMode:UIDatePickerModeTime];

[userTimePicker setMinimumDate:startDate];
[userTimePicker setMaximumDate:endDate];

[userTimePicker setDate:startDate animated:YES];
[userTimePicker reloadInputViews];

}
}

关于iphone - 如何让 UIDatePicker 显示用户选择的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376731/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com