3

タブバーアプリケーションがあり、アプリケーションが実行されていない場合でも、2番目のタブに切り替えて、12:00にアラートをポップアップするとします。

UILocalNotificationのすべてのコードが正しく機能するようになりましたが、そのための最善の方法は、アプリの代理人からの通知を投稿することだと思いました。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Add the tab bar controller's view to the window and display.
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];

    // Handle launching from a notification when the app is NOT running
    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification) {
        [tabBarController setSelectedIndex:1];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"AlertNotification" object:self];
    }
    return YES;
}

次に、SecondViewController.mに次のようになります。

- (void)viewDidLoad {
  [super viewDidLoad];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popUpAlert:) name:@"AlertNotification" object:nil];
}

しかし、これは機能しません。SecondViewControllerのviewDidLoadがまだ呼び出されていないときに通知が送信されたのではないでしょうか?これを解決することは可能ですか?NSNotificationCenterそして、この場合の私の使用方法に同意しますか?

前もって感謝します。

4

3 に答える 3

3

私はすぐにテストプロジェクトを作成し、通知登録を入れることでそれを機能させましたawakeFromNibSecondViewControllerxibファイルで作成されていると仮定します)

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popUpAlert:) name:@"AlertNotification" object:nil];
}
于 2010-12-29T15:58:21.010 に答える
0

君が正しいと思う。ビューコントローラをオブザーバーとして追加する前に通知を投稿しているため、機能しません。

別のアプローチは、アプリがローカル通知から開始されたかどうかを示すために、アプリデリゲートにboolプロパティを追加することです。アプリデリゲートは、アプリ内のどこからでもリクエストできます[[UIApplication sharedApplication] delegate]

于 2010-12-29T14:33:48.303 に答える
0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];
于 2016-04-07T03:09:16.890 に答える