UISwitch状态viewdidload
所以,大家好,我一直在努力,最后一周才得到这个工作。UISwitch状态viewdidload
我有一个应用程序,我需要一个UISwitch打开本地通知。
开关状态的标准是关闭的,但是当警报开启时,我需要在加载时更改此设置。 我试过scheduledLocalNotification,我试过一个BOOL,我试图设置int isOn,当它打开时为0,当它关闭时,没有任何东西似乎为我工作。
的循环,如果我用的是在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];
}
}
请告诉我,如果有你的代码会的任何其他部分喜欢看
但每一个应用程序是从内存的uiswitch卸载时间表示 默认状态(关闭)
您没有存储任何持续性商店交换机的状态。这就是为什么它总是显示为关闭,如果应用程序退出(dealloc被称为你的班级)。
如果你想保存开关状态,即使应用程序不在内存中,那么你需要将该值保存在某个地方。如果需要,您可以使用NSUserDefaults
。
事实上,这就是我所做的,它为我工作! :) – 4FunAndProfit 2013-03-06 18:44:40
你需要发布更多的代码给我们这些变量是什么,你的问题其实是更好的背景,但我想你可能会寻找:
[setAlarm setOn:YES animated:YES];
和
[setAlarm setOn:NO animated:YES];
..如果setAlarm是你的开关名称?
我现在试过了动画:YES,它似乎没有工作。 我会在我的问题中添加一些我的代码:) – 4FunAndProfit 2013-02-27 21:24:56
setAlarm是开关名称 – 4FunAndProfit 2013-02-27 21:34:04
确保setAlarm
确实指向开关,即它不是零,并且它不指向其他开关。并请为您的变量选择其他名称! ;-)
此外,请确保您没有-viewWillAppear
中的代码或在-viewDidLoad
之后调用的某个其他方法重置交换机的值。实际上,您可能想要延迟设置开关和其他UI元素,直到-viewWillAppear
。
我没有其他开关,当我试图 如果(2> 1)NSLog(@“You have notifications”); [setAlarm setOn:YES]; } 其他{ NSLog(@“You have no notifications”); [setAlarm setOn:NO]; } 它确实改变了UISwitch的状态,所以它不是零。 我在viewWillAppear中没有代码:) – 4FunAndProfit 2013-02-27 21:27:41
就像测试一样,将“if(isAlarmOn == 1)”更改为“if 2> 1”。这将帮助你隔离错误。 – melsam 2013-02-27 20:57:59
我不知道从问题什么是不工作。换句话说,如果你只是说[setAlarm setOn:YES],你可以在viewDidLoad中以编程方式改变你的UISwitch的状态;并删除其他的东西?这是收到通知的问题还是什么? – Fraggle 2013-02-27 21:00:40
我试图测试2> 1,我能够以编程方式设置开关。我确实收到了通知,它可以与我的交换机一起工作,但每次应用程序从内存中卸载时,uiswitch都会显示默认状态(关闭)。虽然通知仍然有效,所以这只是一个外观问题。 – 4FunAndProfit 2013-02-27 21:27:01