开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜