特定于设备的加载类别
我有一个 iPhone 应用程序,我想使其通用,大多数视图可以保持不变,但需要针对 iPad 进行一些小的修改。
是否可以根据用户使用的设备加载类别?
或者有更好的方法吗?通用方法(而不是每次创建类的新实例时专门检查,并在两个类之间进行选择)
I have an iPhone app that I'd like to make universal, most views can be kept the same, but there are some minor modifications that need to be made for the iPad.
Is it possible to load a category depending on what device the user is using?
Or is there a better way of doing this? A generic way (rather than specifically checking each time I create a new instance of a class, and choosing between 2 classes)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在运行时通过一些方法混合来做到这一点。举一个简单的例子,如果您想在
UIView
子类中拥有一个与设备相关的drawRect:
方法,您可以编写两个方法,并决定在该类被调用时使用哪个方法。已初始化:这会导致 iPad 上显示红色视图,iPhone 上显示绿色视图。
You could do this with some method swizzling at runtime. As a simple example, if you want to have a device-dependent
drawRect:
method in yourUIView
subclass, you could write two methods and decide which to use when the class is initialized:This should result in a red view on the iPad and a green view on the iPhone.
查看
UI_USER_INTERFACE_IDIOM()
宏,这将允许您根据设备类型分支代码。如果您只想保留每个文件 iPhone 或 iPad,您可能必须创建一个辅助类或抽象超类来返回适当的实例。
Look into the
UI_USER_INTERFACE_IDIOM()
macro, this will allow you to branch your code based on the device type.You may have to create a helper class or abstract superclass which returns the appropriate instance if you want to keep each file iPhone or iPad only.