gpt4 book ai didi

ios - 从 AppDelegate 关闭 uiswitch

转载 作者:行者123 更新时间:2023-11-29 03:03:35 24 4
gpt4 key购买 nike

我在我的 AppDelegate.m 中使用 LocalNotifications如果用户打开了应用程序,通知将以警报的形式出现。AppDelegate.m 接收 clickedButtonAtIndex 事件。无论用户看到的当前 View 如何,警报都会显示,到目前为止一切正常。

但是,当收到事件时,我想更改存在于 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”(从而取消进一步通知)的预期效果,但开关本身在 View 中仍显示为“On”(绿色)。

如何通过我的 AppDelegate.m 中的代码翻转 SettingsPage 上的实际开关,以便它的行为与用户所做的一样(即更改它的视觉效果并执行连接的方法)?

最佳答案

正如 CrimsonChris 所提到的,您似乎每次都在创建 SettingsPage 的新实例,因此您没有看到想要的更改。

你可以触发一个 NSNotification,

[[NSNotificationCenter defaultCenter] postNotificationName:@"ClickedButtonAtIndex1" object:nil];

..并在您的 UIViewController 中收听它。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleIndex1Clicked) name:@"ClickedButtonAtIndex1" object:nil];

用你的 UIViewController 在选择器方法中做它需要做的事情:

-(void)handleIndex1Clicked
{
[self.setPage.alarmSwitch setOn:NO animated:YES];
}

附言。我建议让 extern const NSStrings 保存您的观察者名称。

希望对您有所帮助!

关于ios - 从 AppDelegate 关闭 uiswitch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23018693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com