How to switch between different classes in an universal iPhone / iPad app?
I have a special class that manages gestures and other th开发者_如何学运维ings. It is strongly targeted towards iPhone. On the iPad, I need a 90% different behavior of that class, so I want to split MyController
into MyController_iPhone
and MyController_iPad
.
How would I alloc-init the appropriate class depending on if it's the iPad or iPhone?
You can do something along the following lines:
MyController *controller = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
controller = [[MyController_iPad alloc] init];
} else {
controller = [[MyController_iPhone alloc] init];
}
You might want to subclass the controller for, say, the iPad. When you push/present it, check to see which platform you're on, and if you're on iPad, present the iPad subclass, with the modified behavior. You can use the UI_USER_INTERFACE_IDIOM() macro determine which device you're on.
精彩评论