UIView+Nib.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // UIView+Nib.m
  3. // Created by MacKun on 15/4/25.
  4. // Copyright (c) 2015年 MacKun All rights reserved.
  5. //
  6. #import "UIView+Nib.h"
  7. @implementation UIView (Nib)
  8. #pragma mark - Nibs
  9. + (UINib *)loadNib
  10. {
  11. return [self loadNibNamed:NSStringFromClass([self class])];
  12. }
  13. + (instancetype)loadNibView{
  14. return [[[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil]instantiateWithOwner:nil options:nil] objectAtIndex:0];
  15. }
  16. + (UINib *)loadNibNamed:(NSString*)nibName
  17. {
  18. return [self loadNibNamed:nibName bundle:[NSBundle mainBundle]];
  19. }
  20. + (UINib *)loadNibNamed:(NSString*)nibName bundle:(NSBundle *)bundle
  21. {
  22. return [UINib nibWithNibName:nibName bundle:bundle];
  23. }
  24. + (instancetype)loadInstanceFromNib
  25. {
  26. return [self loadInstanceFromNibWithName:NSStringFromClass([self class])];
  27. }
  28. + (instancetype)loadInstanceFromNibWithName:(NSString *)nibName
  29. {
  30. return [self loadInstanceFromNibWithName:nibName owner:nil];
  31. }
  32. + (instancetype)loadInstanceFromNibWithName:(NSString *)nibName owner:(id)owner
  33. {
  34. return [self loadInstanceFromNibWithName:nibName owner:owner bundle:[NSBundle mainBundle]];
  35. }
  36. + (instancetype)loadInstanceFromNibWithName:(NSString *)nibName owner:(id)owner bundle:(NSBundle *)bundle
  37. {
  38. UIView *result = nil;
  39. NSArray* elements = [bundle loadNibNamed:nibName owner:owner options:nil];
  40. for (id object in elements)
  41. {
  42. if ([object isKindOfClass:[self class]])
  43. {
  44. result = object;
  45. break;
  46. }
  47. }
  48. return result;
  49. }
  50. @end