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 7940437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:23:18+00:00 2026-06-03T23:23:18+00:00

I have done following code in Viewdid Load for creating multiple switch programmatically. float

  • 0

I have done following code in Viewdid Load for creating multiple switch programmatically.

float x =40.0, y=20.0,height=60.0,width=26.0;

    for (int i =0; i < 3; i++) {

        CGRect frame = CGRectMake(x, y, height, width);
        UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
        [switchControl addTarget:self action:@selector(flip:) forControlEvents:UIControlEventTouchUpInside];

        [switchControl setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:switchControl];

        y= y+50.0;
    }

- (IBAction) flip: (id) sender {
    UISwitch *onoff = (UISwitch *) sender;
    NSLog(@"%@", onoff.on ? @"On" : @"Off");
}

After doing this code i am able to create multiple UISwitchs. Now i am not getting how to handle action on every switch. If anyone knows how to handle actions on all switchs please help me. I will appreciate to him/her. Thanks in advance.

  • 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-03T23:23:19+00:00Added an answer on June 3, 2026 at 11:23 pm
    for (int i =0; i < 3; i++) {
    
        CGRect frame = CGRectMake(x, y, height, width);
        UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
    
        //add tag as index 
        switchControl.tag = i;
        [switchControl addTarget:self action:@selector(flip:) forControlEvents: UIControlEventValueChanged];
    
        [switchControl setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:switchControl];
    
        y= y+50.0;
    }
    
    - (IBAction) flip: (id) sender {
        UISwitch *onoff = (UISwitch *) sender;
        NSLog(@"no.%d %@",onoff.tag, onoff.on ? @"On" : @"Off");
        //use onoff.tag , you know which switch you got 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done following code for creating multiple UISwitches programmatically and handle particular switch.
I have just started using jQuery and although following code gets the job done,
I try to get the following done: A WPF application where i have multiple
I have done the following piece of code to add the Hours and thus
I have done the following code for a list of pics, when the user
The following code is a what I have done in order to show a
I have done the following code in JavaScript to put focus on the particular
I have done the following code structure and doing addClass item_box_popup and removeClass item_box
i have done the following code in order to fill a tuple with user
In the following code, I haven';t used any threads. Will creating multiple instances still

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.