2016-03-22 11 views
11

Pracuję z PushNotification na iOS app. Chciałbym pokazać UIalertcontroller, gdy aplikacja otrzyma powiadomienie.Jak wyświetlić UIAlertController z Appdelegate

próbuję ten kod poniżej w AppDelegate:

[self.window.rootViewController presentViewController:alert animated:YES completion:nil]; 

Ale UIAlertcontroller jest wyświetlana w głównym widoku (pierwszy ekran) i dla innych UIViewController Dostałem ostrzeżenie lub awarii aplikacji.

+0

co creash raport –

+0

Ale UIAlertcontroller jest wyświetlana w głównym widoku .... oczywiście dodawania wpisu do kontrolera głównego. Oczywiście spowoduje awarię na innym kontrolerze Uiview, ponieważ próbujesz dodać alert do kontrolera, który nie jest wyświetlany użytkownikowi. –

+1

Tak, wiem, że dodaję kontroler uialert do rootView, a nie do aktywnego widoku, a moim pytaniem jest, w jaki sposób mogę pokazać uialertController w innym sterowniku uiview po otrzymaniu powiadomienia. – Rockers23

Odpowiedz

42

spróbować tej

Objective-C

UIWindow* topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
topWindow.rootViewController = [UIViewController new]; 
topWindow.windowLevel = UIWindowLevelAlert + 1; 

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"APNS" message:@"received Notification" preferredStyle:UIAlertControllerStyleAlert]; 

[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK",@"confirm") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 
    // continue your work 

// important to hide the window after work completed. 
// this also keeps a reference to the window until the action is invoked. 
topWindow.hidden = YES; 
}]]; 

[topWindow makeKeyAndVisible]; 
[topWindow.rootViewController presentViewController:alert animated:YES completion:nil]; 

Swift3

let topWindow = UIWindow(frame: UIScreen.main.bounds) 
topWindow.rootViewController = UIViewController() 
topWindow.windowLevel = UIWindowLevelAlert + 1 
let alert = UIAlertController(title: "APNS", message: "received Notification", preferredStyle: .alert) 
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "confirm"), style: .cancel, handler: {(_ action: UIAlertAction) -> Void in 
    // continue your work 
    // important to hide the window after work completed. 
    // this also keeps a reference to the window until the action is invoked. 
    topWindow.isHidden = true 
})) 
topWindow.makeKeyAndVisible() 
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in }) 

Swift

var topWindow: UIWindow = UIWindow(frame: UIScreen.mainScreen().bounds) 
topWindow.rootViewController = UIViewController() 
topWindow.windowLevel = UIWindowLevelAlert + 1 
var alert: UIAlertController = UIAlertController.alertControllerWithTitle("APNS", message: "received Notification", preferredStyle: .Alert) 
alert.addAction(UIAlertAction.actionWithTitle(NSLocalizedString("OK", "confirm"), style: .Cancel, handler: {(action: UIAlertAction) -> Void in 
// continue your work 
// important to hide the window after work completed. 
// this also keeps a reference to the window until the action is invoked. 
topWindow.hidden = true 
})) 
topWindow.makeKeyAndVisible() 
topWindow.rootViewController.presentViewController(alert, animated: true, completion: { _ in }) 

Szczegółowy opis: http://www.thecave.com/2015/09/28/how-to-present-an-alert-view-using-uialertcontroller-when-you-dont-have-a-view-controller/

+0

dzięki @ Anbu.karthik, to działa :) – Rockers23

+0

Jak chciałbym dodać inny przycisk do UIAlert do szybkiego 3 –

+0

utworzyć obiekt UIAlertAction i obsługiwać akcję –

Powiązane problemy