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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:13:24+00:00 2026-05-27T12:13:24+00:00

First my .h file: @interface SettingsViewController : UIViewController <UINavigationControllerDelegate> { IBOutlet UISwitch *gravaSwitch; …

  • 0

First my .h file:

@interface SettingsViewController : UIViewController <UINavigationControllerDelegate>
{
    IBOutlet UISwitch *gravaSwitch;
    ...
}

@property (retain, nonatomic) UISwitch *gravaSwitch;
...
@end

My viewDidload in .m file (it works):

...
// SET SWITCH BUTTON STATE
keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"TrafficApp" accessGroup:nil];

if ( [[keychain objectForKey:(id)kSecAttrAccount] isEqualToString:@"" ] ) 
    [self.gravaSwitch setOn:FALSE];
else [self.gravaSwitch setOn:TRUE];
...

But my switchChanged doesn’t work and I don’t know why. On IB everything is right connected, it enters in this method but gravaSwitch is always null.

- (IBAction)switchChanged:(id)sender
{
    if ( self.gravaSwitch.on )
    {
        NSLog(@"IF");
        [self.gravaSwitch setOn:FALSE animated:YES];
    }
    else
    {
        NSLog(@"ELSE");
        [self.gravaSwitch setOn:TRUE animated:YES];
    }
}

Regards.

  • 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-05-27T12:13:24+00:00Added an answer on May 27, 2026 at 12:13 pm

    I think the error is the following:

    @interface SettingsViewController : UIViewController <UINavigationControllerDelegate>
    {
        UISwitch *gravaSwitch;
        ...
    }
    
    @property (retain, nonatomic) IBOutlet UISwitch *gravaSwitch;
    ...
    @end
    

    IBOutlet placeholder has to be inserted into @property. Change your code and try to connect your outlet again.

    Edit:

    Try to create your UISwitch programatically. Change your @property as the following:

    @property (retain, nonatomic) UISwitch *gravaSwitch;
    

    Leave @synthesize as is. Then in your viewDidLoad method add your UISwitch (by default on property is FALSE):

    // Width and height are set to zero but they take the default dimension
    UISwitch *yourSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
    // add target and action
    mySwitch addTarget:self action:@selector(yourCustomAction:) forControlEvents:UIControlEventValueChanged];
    self.gravaSwitch = yourSwitch;
    // don't forget because gravaSwitch has already a retain policy
    [release yourSwitch];
    
    // add the switch to the view
    [self.view addSubview:self.gravaSwitch];
    

    Within the same controller, but outside the viewDidLoad method, add the following method:

    - (void)yourCustomAction:(id)sender
    {
        if ( self.gravaSwitch.on )
        {
            NSLog(@"IF");
            [self.gravaSwitch setOn:FALSE animated:YES];
        }
        else
        {
            NSLog(@"ELSE");
            [self.gravaSwitch setOn:TRUE animated:YES];
        }
    }
    
    - (void)dealloc
    {
      self.gravaSwitch = nil; // remember to dealloc the switch!!
      [super dealloc]
    }
    

    You could call self.gravaSwitch = nil; also in viewDidUnload method.

    As an alternative you can set gravaSwicth to assign policy as follow. In this case you haven’t to call self.gravaSwitch = nil; both in dealloc and/or viewDidUnload.

    @property (assign, nonatomic) UISwitch *gravaSwitch;
    

    Hope it helps.

    Edit 2:

    This code works for me. This is the implementation (.m file) for MyViewController.

    @synthesize gravaSwitch;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UISwitch *yourSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
        [yourSwitch addTarget:self action:@selector(yourCustomAction:) forControlEvents:UIControlEventValueChanged];
    
        self.gravaSwitch = yourSwitch;
    
        [yourSwitch release];
    
        [self.view addSubview:self.gravaSwitch];
    }
    
    - (void)yourCustomAction:(id)sender
    {
        if(self.gravaSwitch.on)
        {
            NSLog(@"on");
        }
    
        else
        {
            NSLog(@"off");
        }
    }
    

    where gravaSwicth is declared within MyViewController.h as follows:

    @interface MyViewController : UIViewController
    {
         UISwitch *gravaSwitch;
         ...
    }
    
    @property (retain, nonatomic) UISwitch *gravaSwitch;
    ...
    @end
    

    rembember to call self.gravaSwicth = nil in dealloc in MyViewController.m!!

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

Sidebar

Related Questions

this my first interface in a.h file. @interface EventRow : NSObject { NSString *title;
How to copy the first file in any directory starting with .rar extension?
I have two csv file. First File has date offerid clicks orders Second File
hi guys i have two pipe delimited files,first file contains 1000 records and second
I have 3 files 1) show_createtable.html 2) do_showfielddef.php 3) do_showtble.php 1) First file is
The first batch file launches a command prompt, i need the second command to
Suppose I have two XML files. First XML File: <?xml version=1.0?> <AccessRequest xml:lang=en-US> <AccessLicenseNumber>Your_License</AccessLicenseNumber>
I have the following style in an external CSS file called first.css table {
I traced an oracle process, and find it first open a file /etc/netconfig as
First I have a PHP file that gets data and file from a HTML

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.