【问题标题】:Turn off uiswitch from AppDelegate从 AppDelegate 关闭 uiswitch
【发布时间】: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


    【解决方案1】:

    正如 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];
    }
    

    PS。我建议让 extern const NSStrings 保存您的观察者姓名。

    希望有帮助!

    【讨论】:

    • NSNotificationCenter 与 UILocalNotification 相比如何?当应用程序像 UILocalNotification 一样在后台时它会工作吗?
    • NSNotifcations 是您的应用程序原生的,不会显示给用户。当应用程序在后台时,它们不会运行,只有在它处于活动状态时才会运行。当一个班级见证了另一个班级可能有兴趣了解的某个事件时,使用 NSNotifications。
    • 好的 - 那么我不能使用 NSNotifications - 我的应用程序设置提醒以在后台显示...如果它在前台,它会在本地通知发生时使用警报。
    • 对不起,我误会了。只有当用户点击 LocalNotification 时才会更改此设置吗?如果是这样,您可以在 didFinishLaunching (如果应用程序被终止)或 didReceiveLocalNotification (如果只是在后台)中处理它。这看起来就像您正在做的那样。问题在于“SettingsPage *setPage”,您正在创建 SettingsPage 对象的全新实例,您需要获取已经存在的实例。
    • 很抱歉帮不上忙。也许您可以在处理 LocalNotification 时保存状态,并在显示“设置”页面时适当地设置设置。
    【解决方案2】:

    您似乎获得了一个新的SettingsPage,然后将其alarmSwitch 设置为“关闭”。您可能想要的是获取现有的SettingsPage,而不是使用 alloc init 创建一个新的。

    【讨论】:

    • 似乎合乎逻辑 - 我如何获得对现有 SettingsPage 的引用?
    • 您可以使用@property。您不需要“获取”对现有SettingsPage 的引用。您应该在某处“保留”对它的引用,可能是您的AppDelegate
    • 为什么SettingsPage 不能是处理clickedButtonAtIndex 的东西?
    • AppDelegate.m 创建警报,因此 SettingsPage 永远不会看到它。我相信我需要将 Alert Delegate 设置为 SettingsPage 而不是“self”,但我不知道该怎么做。
    • 只需在AppDelegate 中保留对您的SettingsPage 的引用。你的SettingsPage 来自哪里?如果您向我们展示更多代码,它可能会帮助我们弄清楚究竟是什么让您感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    相关资源
    最近更新 更多