モーニングボタン、アフタヌーンボタン、イブニングボタン、ナイトボタンという名前の4つUIButton
のタグがあり、タグ番号は1、2、3、4で、に割り当てられていviewDidLoad
ます。でuserTimepickerValuechangedメソッドを呼び出しviewDidLoad
ていますが、datepickerに対して呼び出されており、notificationbuttonchangedメソッドがUIButton
sによって呼び出されています。
更新-selectedButtonはNSIntegerです
ユーザーは4つのタグ付きボタンのうちの1つをタップします(notificationButtonChangedが呼び出されます)
ユーザーが日付ピッカーの値を変更します(userTimePickerValueChangedが呼び出されます)
タグnotificationButtonChangedに基づいてボタンをタップすると、カスタム時刻が表示されるdatepickerが呼び出され、datepickerで時刻を選択すると、その時刻はusertimepickerchangedメソッドに格納されますが、selectedButtonは0を返します。
selectedButton = followingButton.tag ..を配置すると、2番目のケースが実行され、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];
}