通用应用程序的命名约定

发布于 2024-10-20 13:09:37 字数 432 浏览 6 评论 0原文

我正在编写一个通用的 iOS 应用程序(iPad 和 iPhone),发现自己的类名称非常长,无法在两个应用程序之间共享:

FamilyViewController_iPhone.h/m
FamilyViewControllerA_iPad.h/m

DetailViewControllerB_iPhone.h/m
DetailViewControllerB_iPad.h/m

同样,这些家伙内部的类具有完整的名称(包括设备)主要是为了让Interface Builder能够轻松使用它们。

我考虑过类似 AControllerA.h 和 BControllerA.h 的内容,其中 A=iPhone 和 B=iPad,但对这个选项也不感兴趣。

在 iOS 通用应用程序中,此类类的常规约定是什么?或者我(希望)错过了一些可以消除这种必要性的东西?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-10-27 13:09:37

我认为通过这样分离你的功能你会走上一条糟糕的道路。虽然 iPad 应用程序显然可以具有与 iPhone 应用程序不同的 UI 结构和设计,但如果您能尝试保持尽可能抽象,那确实是最好的。

我避免在视图控制器中使用平台名称。我的想法是:

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

iPhone 和 iPad 的功能很可能非常相似,或者即使不相同,仍然会有很多重叠。

我也从来没有让我的视图控制器充当我的表控制器。我将该功能保留在单独的对象中。这样,如果 iPhone 应用程序在不同的屏幕上有 2 或 3 个表格,但 iPad 在同一屏幕上显示所有 3 个表格,您只需实例化每个表格控制器,并且所有代码都是可重用的。

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.

北城半夏 2024-10-27 13:09:37

如果您想对 YourClass.xib 和 YourClass-iPad.xib 使用相同的类 YourClass,请查看我使用的这个宏:

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

这样您就可以使用该类本身来获取正确的 XIB 文件:

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

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];
回眸一笑 2024-10-27 13:09:37

我不确定是否有标准方法来处理这个问题。

一种可能性是使用类似类簇的策略。如果 FamilyViewController_iPad 和 FamilyViewController_iPhone 非常相似,则使它们成为抽象 FamilyViewController 类的子类。设置 FamilyViewController 的 -initWithNibName:bundle: 方法,使其返回 ...iPad 或 ...iPhone 版本的实例,具体取决于应用程序运行的设备。那么,在您的大部分代码中,您将只处理 FamilyViewController。当然,您仍然需要在笔尖中使用子类名称,但至少您的代码将更易于阅读。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文