开发者

Naming conventions for Universal Applications

I'm writing a universal iOS application (iPad and iPhone) and find myself with massively long names for classes that can't be shared between the two apps:

FamilyViewController_iPhone.h/m
FamilyViewControllerA_iPad.h/m

DetailViewControllerB_iPhone.h/m
DetailViewControllerB_iPad.h/m
开发者_运维技巧

And likewise, the classes inside of these guys have the complete name (device included) mainly so that Interface Builder can easily use them.

I considered something like AControllerA.h and BControllerA.h where A=iPhone and B=iPad but not excited about that option either.

What is the standing convention for classes like this in an iOS universal application - or am I (hopefully) missing something that obviates this necessity?


I think you're heading down a bad path by separating your functionality like this. While iPad apps can obviously have different UI structures and design from your iPhone apps, but it's really best if you can try to remain as abstract as possible.

I avoid using platform names in my view controllers. I'd have:

FamilyViewController.h
FamilyViewController.m
FamilyViewController.xib (which is used for the iPad UI)
FamilyViewController~iphone.xib

You're most likely going to have incredibly similar functionality for both iPhone and iPad, or if not the same, you'll still have a lot of overlap.

I also never make my view controllers act as my table controllers. I keep that functionality in separate objects. That way, if the iPhone app has 2 or 3 tables on different screens, but the iPad shows all 3 of those tables on the same screen you can just instantiate each of those table controllers and all of the code is reusable.


If you want to use the same class YourClass with YourClass.xib and YourClass-iPad.xib, then check out this macro that I use:

#define getXIB(s) ( (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? @"" #s "-iPad" :@"" #s )

So you can then use the class itself to get the proper XIB file:

YourClass * viewController = [[YourClass alloc] initWithNibName:getXIB(YourClass) bundle:nil];


I'm not sure there's a standard way to handle this.

One possibility is to use a class cluster-like strategy. If FamilyViewController_iPad and FamilyViewController_iPhone are very similar, then make them subclasses of an abstract FamilyViewController class. Set up the -initWithNibName:bundle: method of FamilyViewController such that it returns an instance of either the ...iPad or ...iPhone version, depending on which device the app is running on. In most of your code, then, you'll only deal with FamilyViewController. You'll still need to use the subclass name in your nibs, of course, but at least your code will be easier to read.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜