0

我有 4 个UIButton,分别命名为 morningbutton、下午按钮、晚上按钮和 nightbutton,它的标签号为 1、2、3、4 ..并且分配在viewDidLoad. 我正在调用 userTimepickerValuechanged 方法,viewDidLoad它正在为 datepicker 调用,并且 notificationbuttonchanged 方法由UIButtons 调用。

更新 - 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];
}
4

3 回答 3

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

于 2013-02-10T17:18:23.780 回答
2

-(void)userTimePickerValueChanged:(UIButton *)sender被调用时,发送者是日期选择器,并且您错误地将其转换为 UIButton*。该标签每次都是相同的值,日期选择器本身的标签,可能为 0。

不确定您要在该方法中使用什么功能,但开关可能基于日期选择器的,即选择的日期。

于 2013-02-10T17:19:05.867 回答
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)
  {

请记住,第二个选项更像是一个 hack,所以我让你决定你喜欢做什么,但在第二种情况下,添加注释来解释事情是如何工作的。

于 2013-02-10T17:19:59.520 回答