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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:06:27+00:00 2026-05-26T09:06:27+00:00

I have a UITableView with a UISwitch button in it. I want that when

  • 0

I have a UITableView with a UISwitch button in it. I want that when I run my application default value of the UISwitch button should be ON. If I toggle the switch button from on to off the value in the plist should change to OFF. If I quit the app and then I again run my app the value should again default to ON in the plist.

I have tried using NSUserDefaults, it works i.e when I change the value from ON to OFF the value in NSUserDefaults also changes. But when the app is run again the default value is not set to ON.

- (void)viewDidLoad {
    [super viewDidLoad];
    settings = [[NSMutableArray alloc]initWithObjects:@"Clock",@"Time Format",@"Weather",@"Degrees",nil];
    switchControl = [[UISwitch alloc]initWithFrame:CGRectMake(205, 110, 20, 15) ];
    [self.switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:switchControl];
    NSString *viewdid = @"ON";
    userdefaults = [NSUserDefaults standardUserDefaults];
    [userdefaults setObject:viewdid forKey:@"stateOfSwitch"];

}


- (void)viewWillAppear:(BOOL)animated {
    NSString    *_value= [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfSwitch"];

    if([_value compare:@"ON"] == NSOrderedSame){
        [switchControl setOn:YES animated:YES];
    }
    else {
        [switchControl setOn:NO animated:YES];
    }
    [super viewWillAppear:animated];
}



#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 4;
}
-(void)switchChanged:(id)sender
{

    app= (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];

        NSString *value = @"ON";
    userdefaults = [NSUserDefaults standardUserDefaults];
        if(!switchControl.on){
            value = @"OFF";
            [userdefaults setObject:value forKey:@"stateOfSwitch"];
        }
        [userdefaults setObject:value forKey:@"stateOfSwitch"];
    [userdefaults synchronize];

}


//this is my second class where i am accessing my userdefaults key and depending on that hiding the views
- (void) viewWillAppear:(BOOL)animated
{

    NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfSwitch"];

    if([_value compare:@"ON"] == NSOrderedSame){
        newview.hidden = NO;
        newnewview.hidden =NO;
        firstview.hidden = NO;
        secondview.hidden = NO;
        thirdview.hidden = NO;



    }
    else if([_value compare:@"OFF"] == NSOrderedSame) {

        newview.hidden = YES;
        newnewview.hidden= YES;
        firstview.hidden = YES;
        secondview.hidden = YES;
        thirdview.hidden = YES;
    }

}
  • 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-26T09:06:28+00:00Added an answer on May 26, 2026 at 9:06 am

    In appDelegate did finish loading write below statements

    [[NSUserDefaults standardUserDefaults] setObject:@"OFF" forKey:@"stateOfSwitch"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView that gets populated from CoreData via a controller that implements
I have set up a UITableView with 3 sections that pulls from 3 NSArrays
I have UITableView and it add data from NSMutableArray. So, I want to know
I have a UITableView that has photos, i get these photos from URLs and
I have a UITableView that has a disclosure button on every row. When the
I have UITableView.In that i have created custom cell with accessory button.Now by clicking
I have a UITableView with cells that contain a UISwitch control. It's similar to
In my iOS5 application, I have UITableView that would display cells based on NSString
I have a UITableView cell that is going to have a variable size depending
I have a UITableView which is populated by an array, I have a button

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.