【发布时间】:2014-04-11 17:12:22
【问题描述】:
我在 AppDelegate.m 中使用 LocalNotifications
如果用户打开了应用程序,则通知以警报的形式出现。
AppDelegate.m 接收 clickedButtonAtIndex 事件。无论用户看到的当前视图如何,警报都会显示并且到目前为止一切正常。
但是,当收到事件时,我想更改 UIVIewController 上存在的 UISwitch 的状态。
编辑:添加更多代码 我的应用是这样设置的:
AppDelegate.m 有这个代码:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// Called from the local notification above when the View button is clicked and the app reopens
//called when app is open vs in background
NSLog(@"got notification");
UIApplicationState state=[application applicationState];
if(state==UIApplicationStateActive){
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Notice"
message:notification.alertBody
delegate:self cancelButtonTitle:@"Sleep"
otherButtonTitles:@"Turn Off", nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"clicked button");
if(buttonIndex==1){
SettingsPage *setPage = [[SettingsPage alloc] initWithNibName:nil bundle:nil];
[setPage clickedAlert];
}
}
SettingsPage.m 有这个代码:
@interface SettingsPage()
@property (weak, nonatomic) IBOutlet UISwitch *alarmSwitch;
@end
@implementation SettingsPage
-(IBAction)setAlarm{
//clear all notifications before setting a new one
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//set a new LocalNotification
UILocalNotification *localNotification=localNotification =[[UILocalNotification alloc] init];
if(localNotification!=nil){
localNotification.fireDate=[NSDate dateWithTimeIntervalSinceNow:60]; //seconds
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody=@"Reminder!";
localNotification.hasAction=YES; //fires didreceive event, opens app
localNotification.soundName=UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }
}
-(void)clickedAlert{
NSLog(@"clicked alert");
[self.alarmSwitch setOn:NO animated:YES];
}
这具有将“alarmSwitch”设置为“Off”(从而取消更多通知)的预期效果,但开关本身在视图中仍显示为“On”(绿色)。
如何通过我的 AppDelegate.m 中的代码打开 SettingsPage 上的实际开关,使其行为与用户操作时相同(即更改其视觉效果并执行连接的方法)?
【问题讨论】:
标签: ios objective-c uiswitch