我有4个UIButton
名为morningbutton,afternoonbutton,eveningbutton和nightbutton,其标签号为1,2,3,4 ..并且它在viewDidLoad
中分配。我在viewDidLoad
中调用userTimepickerValuechanged方法,并且正在为datepicker调用它,并且UIButton
调用notificationbuttonchanged方法。
UPDATE - selectedButton是一个NSInteger
用户点击4个标记按钮中的1个 (调用notificationButtonChanged)
用户更改日期选择器的值 (调用userTimePickerValueChanged)
当我点击一个基于标签的按钮时,当我在datepicker中选择一个时间时,我会调用带有自定义时间显示的按钮调用和带有自定义时间显示的datepicker,时间应该存储在usertimepickerchanged方法中..但selectedButton返回0.
如果我把selectedButton = afternoonButton.tag ..然后第二个案件被执行,那么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];
}
答案 0 :(得分:2)
因为您没有为此设置标记,默认情况下它被指定为0.如果您已为其分配了标记,那么在代码中的某个位置将标记0重新分配给它,检查它在何处重新分配。 分配标签超过1000&然后试试。意味着将它们分配给1000,1001,1002,1003只是为了检查。
将您的代码更改为此
- (void)viewDidLoad
{
[super viewDidLoad];
userTimePicker = [[UIDatePicker alloc]init];
[userTimePicker addTarget: self
action: @selector(userTimePickerValueChanged:)forControlEvents: UIControlEventValueChanged];
morningButton = [[UIButton alloc]init];
morningButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
morningButton.tag=1;
afternoonButton = [[UIButton alloc]init];
afternoonButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
afternoonButton.tag=2;
eveningButton = [[UIButton alloc]init];
eveningButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
eveningButton.tag=3;
nightButton =[[UIButton alloc]init];
nightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
nightButton.tag=4;
}
还使用initWithFrame
答案 1 :(得分:2)
如果我理解你正在做什么,时间选择器会调用userTimePickerValueChanged
,所以
-(void) userTimePickerValueChanged:(UIButton *)sender
发件人将是时间选择器,而不是您的任何按钮。这就解释了为什么它不起作用。
要解决此问题,您需要在类中定义一个属性,用于存储实际触发要显示的日期选择器的按钮。然后你会这样做:
-(IBAction)notificationButtonChanged:(UIButton *)control
{
if (control.tag == 1)
{
self.pressedButtonTag = control.tag;
...
和
-(void) userTimePickerValueChanged:(UIButton *)sender
{
switch (self.pressedButtonTag)
{
如果您觉得“懒惰”并且您不想创建特定属性来跟踪按下的按钮,则可以动态地重新分配datePicker.tag
:
-(IBAction)notificationButtonChanged:(UIButton *)control {
if (control.tag == 1) {
userTimePicker.tag = control.tag;
...
和
-(void) userTimePickerValueChanged:(UIDatePicker *)sender {
switch (sender.tag)
{
请记住,第二个选项更像是一个黑客,所以我决定你决定做什么,但在第二个案例中添加注释来解释这个问题是如何工作的。