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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:06:05+00:00 2026-06-08T17:06:05+00:00

I’ve set a switch on my overlay, which appears every time my app launches

  • 0

I’ve set a switch on my overlay, which appears every time my app launches the camera. The switch appears, which is fine. But how do I create the if conditions to command the switch to perform an action when on or off?

//This is the overlay.
- (UIView*)CommomOverlay  {

UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake (30,400,20,20)];
[mySwitch addTarget:self action:@selector(mySwitch)  
forControlEvents:UIControlEventAllTouchEvents];

[view addSubview:mySwitch];
return view;
}

So the switch appears on the overlay, but how do I command it do something when called?

I’ve tried the following

-(void)mySwitch {
  if ([mySwitch.on]){
    execute this..

  }
 }

But the above doesn’t work. I get an error saying “undeclared identifier, did you mean UISwitch?”. So then I replace mySwitch.on with UISwitch.on, then it says ” property on not found on object of type UISwitch”.

I just want to execute my if else method properly. I made the overlay, I made the switch code and it appears on the overly perfectly. But now I want it to do something with an if/else condition. How do I rectify this?

What did I do wrong?

  • 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-08T17:06:08+00:00Added an answer on June 8, 2026 at 5:06 pm

    Glad you decided on the UISwitch, its a much more straightforward solution in my opinion. As for what your problem is, its related to scoping. You are declaring your UISwitch in your overlay initialization, and then the function ends, and you lose the ability to access your UISwitch.

    What you’ll need to do is create a property for it in your .h, then just set it equal to your property inside of your overlay initialization. That should fix your problem.

    EDIT: Going through the problems you mentioned in the comment:

    1) I believe that your declartion should be,

    @property (nonatomic, retain) UISwitch *mySwitch;
    

    Afterwards, you will need to do a @synthesize mySwitch; in your .m file

    2) The reason for this is because you have 2 variables with the same name since it seems that you are redeclaring mySwitch in a few places. I’m guessing these places your code looks like:

    UISwitch *mySwitch = //Stuff;
    

    It should just look like:

    mySwitch = //Stuff;
    

    The reason being that you have already declared it in your .h file, you simply need to initialize or manipulate it. If you declare it once again, you will be overwriting it with a new instance that will once again not exist after you exit your function.

    3) You dont need to set mySwitch = mySwitch for the synthesize, see my above code. Also make sure that your function name is not the same as your variable name!

    Feel free to comment with updates and more questions.
    -Karoly

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.