Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9273957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:14:16+00:00 2026-06-18T16:14:16+00:00

I have 4 UIButton s named morningbutton, afternoonbutton, eveningbutton, and nightbutton where it has

  • 0

I have 4 UIButtons named morningbutton, afternoonbutton, eveningbutton, and nightbutton where it has tag number 1, 2, 3, 4 ..and it is allocated in viewDidLoad. I am calling userTimepickerValuechanged method in viewDidLoad and it is being called for datepicker and notificationbuttonchanged method is called by UIButtons.

UPDATE
– selectedButton is an NSInteger

User taps 1 of 4 tagged buttons
(notificationButtonChanged gets called)

User changes value of date picker
(userTimePickerValueChanged gets called)

When I tap a button based on tag notificationButtonChanged gets called and datepicker with custom time display when I select a time in datepicker that time should get stored in usertimepickerchanged method ..but selectedButton is returning 0.

If I put selectedButton = afternoonButton.tag ..then 2nd case gets executed how should do that all the 4 cases.

Where am I going wrong?

    -(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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:14:18+00:00Added an answer on June 18, 2026 at 4:14 pm

    because you are not set tag for this, it is by default assigned as 0. if you have assigned tag to it, then somewhere in code you are reassigning tag 0 to it, check where it is reassigning.
    assign tag more than 1000 & then try. means assign them 1000,1001,1002,1003 just for checking.

    change your code to this

    - (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;
    
    
     }
    

    also init all buttonss with initWithFrame

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button class named BFToggleButton , derived from UIButton . In my
I have an application which has a number of views which are navigated to
I have a custom UIButton that has the label Name. When the user clicks
In my xib I have taken 4 UIbuttons named button 1, 2, 3 and
In my iPad app, I have a UIButton that is calling an IBAction to
I have a custom class, and that class has a UIButton instance variable. I
I have a UIButton which fires a target action causing one view to transition
I have a UIButton that I create in my sub class ViewController, and add
I have a UIButton that should have 2 statuses - Play and Pause; i.e.
I have a UIbutton title that I set with [[buttons objectAtIndex:selectedButtonTag] setTitle:file forState:UIControlStateNormal]; This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.