19
รับ UIViewController ที่แสดงปัจจุบันบนหน้าจอใน AppDelegate.m
กระแสUIViewControllerบนหน้าจอจำเป็นต้องตอบสนองต่อการแจ้งเตือนแบบพุชจาก APN โดยการตั้งค่ามุมมองป้ายบางส่วน แต่ฉันจะหาUIViewControllerวิธีการapplication:didReceiveRemoteNotification: จากAppDelegate.m? ฉันพยายามใช้self.window.rootViewControllerเพื่อรับการแสดงผลปัจจุบันUIViewControllerอาจเป็นUINavigationViewControllerตัวควบคุมมุมมองหรือประเภทอื่น ๆ และฉันพบว่าvisibleViewControllerคุณสมบัติของUINavigationViewControllerสามารถใช้เพื่อรับUIViewControllerบนหน้าจอ แต่จะทำอย่างไรถ้าไม่ใช่UINavigationViewController? ขอความช่วยเหลือใด ๆ ! รหัสที่เกี่ยวข้องมีดังต่อไปนี้ AppDelegate.m ... - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { //I would like to find out which view controller is on the screen here. UIViewController *vc = [(UINavigationViewController *)self.window.rootViewController visibleViewController]; [vc performSelector:@selector(handleThePushNotification:) withObject:userInfo]; } ... ViewControllerA.m - (void)handleThePushNotification:(NSDictionary *)userInfo{ …