rostgaard
rostgaard

Reputation: 335

UISwitch state in viewdidload

So guys, i've been trying literally for the last week to get this to work.

I have a app that i need a UISwitch to turn on a local notification.
Standard for the switchs state is off, but i need to change this, on load, when the alarm is on. I've tried scheduledLocalNotification, i've tried a BOOL and i've tried to set int isOn, to 1 when it's on and 0 when it's off, nothing seems to be working for me.

The if loop i use is in ViewDidLoad and looks like this:

if (isAlarmOn == 1) {
        NSLog(@"You have notifications");
        [setAlarm setOn:YES];

    }
    else{
        NSLog(@"You have no notifications");
        [setAlarm setOn:NO];
    } 

No matter which strategy i try to use, i doesn't seem to work, hope i can get some help from your guys.

Some more code:

The code for my setAlarm

- (IBAction)setAlarm:(id)sender {
    NSArray *notificationArray=[[UIApplication sharedApplication] scheduledLocalNotifications];


        if (setAlarm.on) {
            [alarmStatus setText:@"Alarmen er tændt"];
            //Metode til at undersøge om klokken før eller efter officiel solned
            NSTimeInterval checkTime = [[astronomicalCalendar sunset] timeIntervalSinceDate:[NSDate date]];

            NSLog(@"Alarmen er tændt");

            if (checkTime >= 0) {
                [self scheduleNotificationToday];
            }
            else{
                [self scheduleNotificationTomorrow];
            }

             NSLog(@"antal alamer %@", notificationArray);
            isAlarmOn = 1;

                }
        else {
            //Metode til at slette alle notifikationer
             [[UIApplication sharedApplication] cancelAllLocalNotifications];
            NSLog(@"Alarmen er slukket");

            NSLog(@"antal alamer %@", notificationArray);

            isAlarmOn = 0;
        }
    }

My UILocalNotification code

    - (void)scheduleNotificationToday {
        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls != nil) {
    //Just some stuff determine which time to alert on. 

                UILocalNotification *notif = [[cls

 alloc] init];
            notif.fireDate = [gregorian dateByAddingComponents:traekFraDag toDate:[astronomicalCalendar sunset] options:0];
            notif.timeZone = [NSTimeZone defaultTimeZone];

            notif.alertBody = @"Nu går solen snart ned";
            notif.alertAction = @"Show me";
            notif.soundName = @"Waterline.caf";
            notif.applicationIconBadgeNumber = 1;


            [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        }
    }

Please tell me if there is any other part of the code you would like to see

Upvotes: 0

Views: 793

Answers (3)

Fraggle
Fraggle

Reputation: 8727

but every time the app is unloaded from the memory the uiswitch shows the default state (off)

You are not storing the state of your switch in any persistant store. That is why it always shows as off if the app exits (dealloc is called for your class).

If you want to save the switch state even when the app is not in memory, then you'll need to save that value somewhere. You can use NSUserDefaults for this if you want.

Upvotes: 0

Caleb
Caleb

Reputation: 125017

Make sure that setAlarm really does point to the switch, i.e. it's not nil and it doesn't point to some other switch. And please choose some other name for your variable! ;-)

Also, make sure that you don't have code in -viewWillAppear or some other method that's called after -viewDidLoad that resets the switch's value. In fact, you might want to defer setting up the switch and other UI elements until -viewWillAppear.

Upvotes: 1

Tommy Devoy
Tommy Devoy

Reputation: 13549

You need to post more code to give us better context on what these variables are and what your question actually is, but I think you might be looking for:

[setAlarm setOn:YES animated:YES];

and

[setAlarm setOn:NO animated:YES];

..if setAlarm is your switch name??

Upvotes: 1

Related Questions