【问题标题】:Using Array from ViewController in AppDelegate在 AppDelegate 中使用 ViewController 中的数组
【发布时间】:2013-07-09 23:30:12
【问题描述】:

我有一个存储在 ViewController.m 中的数组。但是当应用程序进入后台时,我想从数组中发布通知。

所以我的 NSMutableArray “列表”是在 ViewController.m 中创建的,但我需要在 AppDelegate.m 中使用

- (void)applicationDidEnterBackground:(UIApplication *)application




for (NSString *thing in list) {
    UILocalNotification *notif = [[UILocalNotification alloc] init];
    notif.alertBody = thing.text;
    [[UIApplication sharedApplication] presentLocalNotificationNow:notif];

【问题讨论】:

    标签: ios arrays object viewcontroller appdelegate


    【解决方案1】:

    如果 ViewController.m 是你的主要 VC:

    ViewController *yourVC = (ViewController*)self.window.rootViewController;
    yourVC.yourMutableArray = whateverYouWant;
    

    我建议将数据保存在 NSUserDefaults 中,然后您可以轻松访问它并在任何地方读/写。顺便说一句,在 appDelegate 中从该方法显示 localNotifications 是一个坏主意。当有人试图离开应用程序时,应用程序商店不会喜欢立即通知......如果你甚至可以这样做的话。

    【讨论】:

    • 上面的代码应该放在应用程序委托中......但是你应该按照我所说的 NSUserDefaults 来代替。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多