简体   繁体   English

在viewdidload中的UISwitch状态

[英]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. 我有一个应用程序,我需要一个UISwitch打开本地通知。
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. 我已经尝试过scheduledLocalNotification,我尝试了一个BOOL,我试图设置int isOn,当它打开时为1,当它关闭时为0,似乎没有什么对我有用。

The if loop i use is in ViewDidLoad and looks like this: 我使用的if循环是在ViewDidLoad中,看起来像这样:

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 我的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 我的UILocalNotification代码

    - (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 如果您希望看到代码的其他任何部分,请告诉我

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?? ..if setAlarm是你的开关名称?

Make sure that setAlarm really does point to the switch, ie it's not nil and it doesn't point to some other switch. 确保setAlarm确实指向开关,即它不是nil并且它不指向其他开关。 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. 此外,请确保你没有代码-viewWillAppear或者某个之后调用其他方法-viewDidLoad一个重置开关的价值。 In fact, you might want to defer setting up the switch and other UI elements until -viewWillAppear . 实际上,您可能希望推迟设置开关和其他UI元素,直到-viewWillAppear

but every time the app is unloaded from the memory the uiswitch shows the default state (off) 但每次从内存中卸载应用程序时,uiswitch都会显示默认状态(关闭)

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). 这就是为什么它总是显示为关闭应用程序退出(为您的类调用dealloc)。

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. 如果需要,您可以使用NSUserDefaults

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM