ios - 从 AppDelegate 关闭 uiswitch

标签 ios objective-c uiswitch

我在我的 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/

相关文章:

objective-c - Apple Metal 逐元素矩阵乘法(Hadamard 积)

ios - 用于在单独的 View Controller 中激活或停用图像阵列的 UIswitch

ios - 使用 CorePlot 框架时显示 int 值

ios - -[UIView setAdUnitID1 :]: unrecognized selector sent to instance

iphone - iOS 上特定于应用程序的数据保护

ios - 如何正确地将单例分配给 iOS 中的变量?

iphone - 水平滚动 UITableView

ios - 对具有自己的转换的 ViewController 的不平衡调用

ios - 如何知道 UITableView 中 "On"的 UISwitch 的数量?

ios - 如何在覆盖上设置 UISwitch?