0

所以,伙计们,我上周一直在努力让它发挥作用。

我有一个应用程序,我需要一个 UISwitch 来打开本地通知。
开关状态的标准是关闭的,但是当警报打开时,我需要在负载时更改它。我已经尝试了 scheduleLocalNotification,我尝试了 BOOL,我尝试将 int isOn 设置为 1,当它打开时设置为 0,当它关闭时,似乎没有什么对我有用。

我使用的 if 循环在 ViewDidLoad 中,如下所示:

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

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

无论我尝试使用哪种策略,我似乎都不起作用,希望我能得到你们的帮助。

更多代码:

我的 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;
        }
    }

我的 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];
        }
    }

请告诉我您是否想查看代码的任何其他部分

4

3 回答 3

1

您需要发布更多代码,以便我们更好地了解这些变量是什么以及您的问题实际上是什么,但我认为您可能正在寻找:

[setAlarm setOn:YES animated:YES];

[setAlarm setOn:NO animated:YES];

..如果 setAlarm 是您的开关名称?

于 2013-02-27T21:11:19.160 回答
1

确保它setAlarm确实指向开关,即它不是 nil 并且它不指向其他开关。请为您的变量选择其他名称!;-)

此外,请确保您没有代码或在重置开关值-viewWillAppear之后调用的其他方法。-viewDidLoad事实上,您可能希望将开关和其他 UI 元素的设置推迟到-viewWillAppear.

于 2013-02-27T21:14:36.757 回答
0

但是每次从内存中卸载应用程序时,uiswitch 都会显示默认状态(关闭)

您没有将交换机的状态存储在任何持久存储中。这就是为什么如果应用程序退出它总是显示为关闭(为您的班级调用了dealloc)。

如果即使应用程序不在内存中也想保存开关状态,那么您需要将该值保存在某处。NSUserDefaults如果你愿意,你可以使用它。

于 2013-03-04T01:34:59.970 回答