Posted By

yar on 08/21/12


Tagged

iphone iPad ios


Versions (?)

Alloc Method to Choose Between Regular and Phone


 / Published in: Objective C
 

This method will alloc the current class unless the device is a phone (also defined here). Then it will try to alloc the class with the suffix "Phone."

  1. #define IS_PHONE UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone
  2.  
  3. #define ALLOC_PER_DEVICE() id retVal = nil; \
  4. NSString *className = NSStringFromClass(self);\
  5. if (IS_PHONE && ![className hasSuffix:@"Phone"]) {\
  6. className = [NSString stringWithFormat:@"%@Phone", className];\
  7. Class newClass = NSClassFromString(className);\
  8. retVal = [newClass alloc];\
  9. }\
  10. if (!retVal)\
  11. retVal = [super alloc];\
  12. assert(retVal != nil);\
  13. return retVal\
  14.  
  15. // Sample Usage
  16. + (id) alloc {
  17. ALLOC_PER_DEVICE();
  18. }

Report this snippet  

You need to login to post a comment.