Getting a reference to the UIApplication delegate

2019-02-02 02:22发布

I'm writing my first iPhone application and I'm having trouble switching views. I have 2 views and a reference to each in the AppDelegate (an instance of UIApplicationDelegate). I create instances of both in the applicationDidFinishLaunching and immediately show the first view. This works fine.

The problem is the reference to the other view is in the AppDelegate and I can't figure out how to get a reference to it so I can switch to the other view. Is there a way to get a reference to the main UIApplication or UIApplicationDelegate objects?

2条回答
三岁会撩人
2楼-- · 2019-02-02 03:05

Use:

[[UIApplication sharedApplication] delegate];
查看更多
欢心
3楼-- · 2019-02-02 03:07

Yes, UIApplication is a singleton, and uses the normal singleton pattern for Objective-C:

[UIApplication sharedApplication];

You can get your delegate class directly from it:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
查看更多
登录 后发表回答