1.View:
XIB:
+ (instancetype)loadFromNib {
NSString *myclass = NSStringFromClass([self class]);
// 封装Xib的加载过程
return [[NSBundle mainBundle] loadNibNamed:myclass owner:nil options:nil].firstObject;
}
- (void)awakeFromNib {
[super awakeFromNib];
// add Initialization code
}
//调用
[xxx loadFromNib];
OC纯代码:
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//add init code
return self;
}
//调用
[[xxx alloc] initWithFrame:...];
2.NSObject
OC纯代码:
- (instancetype)initWithxxx:(xxx)xxx {
self = [super init];
if (self) {
//add init code;
}
return self;
}
//调用
[[xxx alloc] initWithxxx:...];
//单例
+ (instancetype)sharedInstance {
static ClassName *className;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
className = [[ClassName alloc] init];
});
return className;
}
//如果作为父类
+ (instancetype)sharedInstance {
id instance = objc_getAssociatedObject(self, @"instance");
if (!instance) {
instance = [[super allocWithZone:NULL] init];
objc_setAssociatedObject(self, @"instance", instance, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return instance;
}
//调用
[xxx sharedInstance];
3.ViewController
Story Board:
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
// Pass the selected object to the new view controller.
// Push the view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}