Skip to content

Instantly share code, notes, and snippets.

@andrealufino
Last active December 13, 2015 23:49
Show Gist options
  • Save andrealufino/4994648 to your computer and use it in GitHub Desktop.
Save andrealufino/4994648 to your computer and use it in GitHub Desktop.
This is an example of an Utility class which is useful to manage multiple xib files for different views for iPhone 4 and iPhone 5. This is an example of use : MyViewController *controller = [[MyViewController alloc] initWithNibName:[Utility nibName:@"MyNibName"] bundle:nil]; The class check if the xib to load will have to be the one for the iPho…
//
// SWPUtility.h
//
//
// Created by Andrea Lufino on 06/02/13.
// Copyright 2013 Andrea Lufino. All rights reserved.
#import <Foundation/Foundation.h>
@interface SWPUtility : NSObject
//Class method
+ (SWPUtility *) shared;
/*
* Instance methods
*/
+ (NSString *)nibNamed:(NSString *)nibName;
@end
//
// SWPUtility.m
//
//
// Created by Andrea Lufino on 06/02/13.
// Copyright 2013 Andrea Lufino. All rights reserved.
#import "SWPUtility.h"
@implementation SWPUtility
#pragma mark - Singleton methods
+ (SWPUtility *)shared {
static SWPUtility *_shared;
if(!_shared) {
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^ {
_shared = [[super allocWithZone:nil] init];
});
}
return _shared;
}
+ (id)allocWithZone:(NSZone *)zone { return [self shared]; }
- (id)copyWithZone:(NSZone *)zone { return self; }
#if (!__has_feature(objc_arc))
- (id)retain { return self; }
- (unsigned)retainCount { return UINT_MAX; }
- (void)release { }
- (id)autorelease { return self; }
#endif
#pragma mark - Custom methods
//check the device and load the correct view
+ (NSString *)nibNamed:(NSString *)nibName {
[self shared];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if ([[UIScreen mainScreen] bounds].size.height > 480.0f) {
NSLog(@"Load iPhone 5 views");
return [NSString stringWithFormat:@"%@_iPhone5", nibName];
} else {
NSLog(@"Load iPhone 4 and less views");
return [NSString stringWithFormat:@"%@_iPhone4", nibName];
}
} else {
NSLog(@"Recognized an iPad. Load iPhone 5 views");
return [NSString stringWithFormat:@"%@_iPhone5", nibName];
//manage iPad view, if you have one
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment