开发者

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

开发者 https://www.devze.com 2023-02-28 04:12 出处:网络
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 RootViewCont开发

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 RootViewCont开发者_运维百科roller to hold a reference to the AppDelegate)


[[[UIApplication sharedApplication] delegate] someMethod];

Works like a charm!


You can get access to the app delegate from any controller using

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]
0

精彩评论

暂无评论...
验证码 换一张
取 消