无法在app delegate和storyboard之间传递数据

时间:2014-07-04 07:25:25

标签: ios objective-c

当我收到本地通知时,我需要将一个变量设置为NO。在App Delegate中我运行:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if ([[notification alertBody] isEqualToString:@"Time To Wake Up"]) {
    NLAlarmViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"AlarmVC"];
    [vc setStarted:NO];
    }
}

在视图控制器类中:

- (IBAction)twoTapsDetected:(UITapGestureRecognizer *)sender {
    NSLog(@"Two taps detected");
    if (started != YES ) {
        NSLog(@"Not Started");
    }
    else if (started != NO) {
        NSLog(@"Started");
    }
}

在日志中我已经开始了。但在App Delegate中我将其设置为NO。当我在Xcode中开始断点= YES时,应该是NO。也许我做错了什么?

1 个答案:

答案 0 :(得分:0)

在显示通知的appdelegate中执行带有标识符的segue。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if ([[notification alertBody] isEqualToString:@"Time To Wake Up"]) {
    [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:self];
    }
}

然后使用故事板委托方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YourSegueIdentifier"]) {
        // Get reference to the destination view controller
    NLAlarmViewController *vc = (NLAlarmViewController *)[segue destinationViewController];

        // Pass any objects to the view controller here, like...
     [vc setStarted:NO];
    }
}