views:

74

answers:

1

How do I access the shared delegate or the device specific "delegate" in a Universal App?

I want to store properties on the Shared delegate and put basic logic there, but if I want to do, say iPhone specific stuff on the iPhone delegate, I would assume that I need to access the two delegates separately. Is this correct?

How do I access these delegates in code?

+3  A: 

I'm not sure what you mean by device-specific delegates. I'm assuming that by "shared delegate" you're referring to your application delegate. If you needed something specific to iPhone or iPad, you could do this:

BOOL isiPad = NO;
if ([UIDevice instancesRespondToSelector:@selector(userInterfaceIdiom)]) {
    UIUserInterfaceIdiom idiom = [[UIDevice currentDevice] userInterfaceIdiom];

    if (idiom == UIUserInterfaceIdiomPad) {
        isiPad = YES;
    }
}

if (isiPad) {
    // iPad-specific stuff
} else {
    // iPhone-specific stuff
}

That's better than using #defines because you can compile one universal app to work across all iOS devices.

EDIT: Added some introspection to prevent this from crashing on iPhone OS 3.1.x and earlier. Thanks, Bastian.

Jeff Kelley
When upgrading an older project for iPad, XCode creates a shared delegate. I assumed that a new Universal application does the same. Apparently not. My mistake.
Moshe
Please note that this will crash on 3.1.x devices because userInterfaceIdiom don't exists there.You would have to check first if currentDevice has this selector.
Bastian
I just checked again and there is a define that you can use already defined in UIDevive.h:#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
Bastian
Thanks, Bastian. I've updated the code, but I'll leave out the macro to give a better sense of what's going on.
Jeff Kelley