简体   繁体   中英

how to call an AppDelegate method from RootViewController without passing a delegate?

how to call an AppDelegate method from RootViewController without passing a delegate?

Just wondering whether there is a way to do this? (or do I need to create a delegate object in the RootViewController to hold a reference to the AppDelegate)

[[[UIApplication sharedApplication] delegate] someMethod];

奇迹般有效!

您可以使用以下任何方式从任何控制器访问应用程序委托:

MyDelegate* aDelegate = (MyDelegate *)[[UIApplication sharedApplication] delegate];

This happens so frequently, that I always add a method to MyCustomAppDelegate to do this for me (so I don't have a lot of casting in my code.

@implementation MyCustomAppDelegate ()
- (MyCustomAppDelegate *)appDelegate {
  return (MyCustomAppDelegate *)[[UIApplication sharedApplication] delegate];
}
@end

now anywhere i can call

[MyCustomAppDelgate appDelegate]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM