Question

J'ai une application et TabBar disons que je veux passer à la deuxième onglet et une alerte à apparaitre 12:00, même si mon application ne fonctionne pas.

Je suis tout le code pour fonctionner correctement UILocalNotification, mais je pensais que la meilleure façon de le faire serait en affichant une notification du délégué de l'application:

- (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;
}

Alors, dans mon SecondViewController.m, je:

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

Mais cela ne fonctionne pas. Je soupçonne que la notification est envoyée alors n'a pas encore été appelée la viewDidLoad du SecondViewController, non? Est-il possible de nous en sortir? Et acceptez-vous sur mon approche de l'utilisation NSNotificationCenter dans ce cas?

Merci à l'avance.

Était-ce utile?

La solution

J'ai rapidement créé un projet de test et obtenu ce travail en mettant l'enregistrement de notification awakeFromNib (en supposant SecondViewController est créé dans un fichier xib)

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

Autres conseils

Je suppose que, vous avez raison. Il ne fonctionne pas parce que vous envoyez des messages la notification avant d'ajouter le contrôleur de vue en tant qu'observateur pour elle.

Une autre approche serait d'ajouter une propriété bool au délégué de l'application pour indiquer si l'application a été lancée à partir de la notification locale. Le délégué de l'application peut être demandée partout dans l'application avec [[UIApplication sharedApplication] delegate].

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top