开发者

How to manage separate app delegates in universal application

开发者 https://www.devze.com 2023-02-11 05:45 出处:网络
in Universal application it gives you iphone AppDelegate and ipad AppDelegate separately. Now, how do you put in the logic to determine which app delegate to use, how does it know which one to instan

in Universal application it gives you iphone AppDelegate and ipad AppDelegate separately.

Now, how do you put in the logic to determine which app delegate to use, how does it know which one to instantiate and work with as the default template doesnt state. If I were to write in the iPhone appDelegate, how do开发者_JS百科 i know that this will only run for the iPhone iOS for example....


You don't instantiate an app delegate yourself. Your Info.plist file tells which Nib file to load when the application runs on an iPhone, or on an iPad. And each Nib file says to make an object of the corresponding class. Therefore, the right app delegate class is automatically chosen, and you can access it by [[UIApplication sharedApplication] delegate].

In most cases, therefore, you need not worry about this problem. You can be sure that all methods in, say, the iPhone app delegate will run when running on an iPhone.

One case where you might care is when you are sending a message to the delegate or reading its properties from outside of the delegate class (Although this case is almost always not desirable).

In that case, to suppress compiler warnings, you may want to do something like this:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    [(AppDelegate_iPad *)[[UIApplication sharedApplication] delegate] specificMessage];


You can do that in your main.m like this.

int main(int argc, char *argv[])
{
    @autoreleasepool {

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {

         return UIApplicationMain(argc, argv, nil, NSStringFromClass([myipadAppDelegate class]));
    }
    else{

    return UIApplicationMain(argc, argv, nil,  NSStringFromClass([myiphoneAppDelegate class]));
    }
  }
}
0

精彩评论

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