简体   繁体   English

为iPad和iPhone运行时创建不同的@properties

[英]Create different @properties for iPad and iPhone runtime

I have a universal app and for each device (iPad, iPhone) have different sets of properties like this: 我有一个通用应用程序,每个设备(iPad,iPhone)都有不同的属性集,如下所示:

//iPhone
@property (nonatomic, strong) CategoriesViewController *categoriesViewController;
//iPad
@property (nonatomic, strong) UIPopoverController *categoriesPopoverController;

But I don't want to waste memory synthesizing properties for each device. 但是我不想浪费每个设备的内存合成属性。 Like in my case when user opens app with iPhone, only iPhone specific @properties should be created. 就像在我用用户使用iPhone打开应用程序的情况下一样,只应创建特定于iPhone的@properties。 Also wice-versa for app running on iPad. 反之亦然,适用于在iPad上运行的应用。

Is there any directive in compile time to defer this, something like that: 编译时是否有任何指令可以推迟执行,例如:

#if UI_USER_INTERFACE_IDIOM() = iPhone
//create iPhone properties
#elseif UI_USER_INTERFACE_IDIOM() = iPad
//create iPad properties

Is there any way to do this or is there a better way to handle this? 有什么办法可以做到这一点,还是有更好的方法来解决呢?

Thanks! 谢谢!

There is no way to do exactly what you're asking. 无法完全按照您的要求进行操作。

  1. Is this extra memory usage really significant? 这种额外的内存使用真的重要吗? A property like that is only 4 bytes per instance of your object. 这样的属性每个对象实例只有4个字节。 If it's in something like a controller, where you have only one (or a few) instances, then this is not even worth talking about. 如果它在控制器之类的地方,而您只有一个(或几个)实例,那么这甚至不值得讨论。

  2. If you really need to do this, consider making an abstract base class, then make subclasses for the iPad and iPhone versions. 如果确实需要执行此操作,请考虑创建一个抽象基类,然后为iPad和iPhone版本创建子类。 At runtime, use UI_USER_INTERFACE_IDIOM() to decide which class to instantiate. 在运行时,使用UI_USER_INTERFACE_IDIOM()来确定要实例化的类。

This is standard Objective C way to solve such issues: 这是解决此类问题的标准Objective C方法:

  1. Create @protocol reflecting common behavior of both controllers. 创建反映两个控制器共同行为的@protocol。
  2. Both controllers should conform this protocol, ie implement behavior. 两个控制器都应遵守该协议,即实现行为。
  3. Create ONE @property like: 创建一个@property,例如:

    @property (nonatomic, retain) id < YourProtocolName > categoriesController. @property(非原子的,保留的)id <YourProtocolName> CategoriesController。

  4. Instantiate iPad or iPhone related controller into categoriesController. 将与iPad或iPhone相关的控制器实例化为类别。

This make your architecture more elegant. 这使您的体系结构更加优雅。 Have a nice day ;) 祝你今天愉快 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM