我有 4 个UIButton
,分别命名为 morningbutton、下午按钮、晚上按钮和 nightbutton,它的标签号为 1、2、3、4 ..并且分配在viewDidLoad
. 我正在调用 userTimepickerValuechanged 方法,viewDidLoad
它正在为 datepicker 调用,并且 notificationbuttonchanged 方法由UIButton
s 调用。
更新 - selectedButton 是一个 NSInteger
用户点击 4 个标记按钮中的 1 个(调用 notifyButtonChanged)
用户更改日期选择器的值(调用 userTimePickerValueChanged)
当我点击一个基于标签 notificationButtonChanged 的按钮时,当我在 datepicker 中选择一个时间时,将调用具有自定义时间显示的 datepicker,该时间应该存储在 usertimepickerchanged 方法中..但是 selectedButton 返回 0。
如果我把 selectedButton = meadowButton.tag ..then 第二种情况执行,那么所有 4 种情况应该怎么做。
我哪里错了?
-(IBAction)notificationButtonChanged:(UIButton *)control
{
if (control.tag == 1)
{
[userTimePicker reloadInputViews];
[components setHour:6];;
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:10];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
}
if (control.tag == 2)
{
[userTimePicker reloadInputViews];
[components setHour:12];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:14];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
}
if (control.tag == 3)
{
[userTimePicker reloadInputViews];
[components setHour:15];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:18];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
}
if (control.tag == 4)
{
[userTimePicker reloadInputViews];
[components setHour:19];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:22];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
}
}
-(void) userTimePickerValueChanged:(UIButton *)sender
{
selectedButton = [sender tag];
switch (selectedButton)
{
case 1:
{
NSUserDefaults *userSelectedMorningTime = [NSUserDefaults standardUserDefaults];
NSDate *selectedMorningTime = [userTimePicker date];
NSLog (@"SETTING: morningTimeKey");
[userSelectedMorningTime setObject:selectedMorningTime forKey:@"morningTimeKey"];
[userSelectedMorningTime synchronize];
}
break;
case 2:
{
NSUserDefaults *userSelectedAfternoonTime = [NSUserDefaults standardUserDefaults];
NSDate *selectedAfternoonTime = [userTimePicker date];
NSLog (@"SETTING: afternoonTimeKey");
[userSelectedAfternoonTime setObject:selectedAfternoonTime forKey:@"afternoonTimeKey"];
[userSelectedAfternoonTime synchronize];
}break;
case 3:
{
NSUserDefaults *userSelectedEveningTime = [NSUserDefaults standardUserDefaults];
NSDate *selectedEveningTime =[userTimePicker date];
NSLog (@"SETTING: eveningTimeKey");
[userSelectedEveningTime setObject:selectedEveningTime forKey:@"eveningTimeKey"];
[userSelectedEveningTime synchronize];
}break;
case 4:
{
NSUserDefaults *userSelectedNightTime = [NSUserDefaults standardUserDefaults];
NSDate *selectedNightTime =[userTimePicker date];
NSLog (@"SETTING: nightTimeKey");
[userSelectedNightTime setObject:selectedNightTime forKey:@"nightTimeKey"];
[userSelectedNightTime synchronize];
}
break;
default:
break;
}
}
viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
userTimePicker = [[UIDatePicker alloc]init];
[userTimePicker addTarget: self
action: @selector(userTimePickerValueChanged:)forControlEvents: UIControlEventValueChanged];
morningButton = [[UIButton alloc]init];
morningButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
afternoonButton = [[UIButton alloc]init];
afternoonButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
eveningButton = [[UIButton alloc]init];
eveningButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
nightButton =[[UIButton alloc]init];
nightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
-(IBAction)settingsButtonChanged:(UIButton *)sender
{
settingsView.frame = CGRectMake(25.0, 60.0, 280.0, 400.0);
morningButton.tag = 1;
morningButton.frame = CGRectMake(25.0, 20.0, 80.0, 40.0);
morningButton.backgroundColor = [UIColor brownColor];
[morningButton setTitle:@"Morning" forState:UIControlStateNormal];
afternoonButton.tag =2;
afternoonButton.frame = CGRectMake(185.0, 20.0, 80.0, 40.0);
afternoonButton.backgroundColor = [UIColor grayColor];
[afternoonButton setTitle:@"Afternoon" forState:UIControlStateNormal];
eveningButton.tag = 3;
eveningButton.frame = CGRectMake(25.0, 70.0, 80.0, 40.0);
eveningButton.backgroundColor = [UIColor greenColor];
[eveningButton setTitle:@"Evening" forState:UIControlStateNormal];
nightButton.tag = 4;
nightButton.frame = CGRectMake(185.0, 70.0, 80.0, 40.0);
nightButton.backgroundColor = [UIColor orangeColor];
[nightButton setTitle:@"Night" forState:UIControlStateNormal];
userTimePicker.frame = CGRectMake(60.0, 194.0, 150.0, 180.0);
userTimePicker.datePickerMode = UIDatePickerModeTime;
[morningButton addTarget:self action:@selector(notificationButtonChanged:) forControlEvents:UIControlEventTouchUpInside];
[afternoonButton addTarget:self action:@selector(notificationButtonChanged:) forControlEvents:UIControlEventTouchUpInside];
[eveningButton addTarget:self action:@selector(notificationButtonChanged:) forControlEvents:UIControlEventTouchUpInside];
[nightButton addTarget:self action:@selector(notificationButtonChanged:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:remedyViewBackButton];
[settingsView addSubview:morningButton];
[settingsView addSubview:afternoonButton];
[settingsView addSubview:eveningButton];
[settingsView addSubview:nightButton];
[settingsView addSubview:userTimePicker];
[remedyView addSubview:remedyViewBackButton];
[remedyView addSubview:settingsView];
}