XIB总结(代码加载xib或xib拖xib)

view.xib的说明
View的custom cass是关联自身的 File’s owner是关联任意类的

方式一.xib拖xib 用File’s owner
方式二.代码加载,不向某个控制器关联控件 用View的custom class

一.代码加载XIB


1).只有xib文件
1.只有View1.xib文件
2.File’s Owner 和View1的Custom class都未设置

加载View的方法

 UIView *v1 =  [[[NSBundle mainBundle] 
loadNibNamed:@"View1" owner:nil options:nil] lastObject];

2).有xib****( .h 和.m )
****文件
1.有View11.xib View11.h View11.m文件
2.File’s Owner 和View1的Custom class都未设置

加载View的方法

UIView *v11 =  [[[NSBundle mainBundle] 
loadNibNamed:@"View11" owner:nil options:nil] lastObject];

3).有xib文件,设置了File’s Owner
1.有View2.xib文件
2.A.File’s Owner 为ViewController (File’s Owner关联ViewController.xib,可以向 VIewController.h或ViewCOntroller.m拖线)
B. 或者设置为UIViewController

3.View2的Custom class未设置

A种情况 某个ViewController的View
ViewControler加载View方法

    UIView *v2 =  [[[NSBundle mainBundle] 
loadNibNamed:@"View2" owner:self options:nil] lastObject];

B种情况 公共的
加载View方法

 UIView *v2 = [[UIViewController alloc] initWithNibName:
@"View2" bundle:nil].view;
  1. .有xib( .h 和.m ) 文件,设置了File’s Owner 和 View的Custom Class**

1.有View3.xib文件 View3.h View3.m
2.File’s Owner设置为 A.ViewContoller
或者 B.UIViewController)
3.View3的Custom class 设置为 VIew3 (xib可以向 View3.h或View3.m拖线)

A种情况 某个ViewController的View
ViewControler加载View方法

   UIView *v3 =  [[[NSBundle mainBundle] loadNibNamed:
@"View3" owner:self options:nil] lastObject];

B种情况 公共的 需要将view和File’s Owner连线
加载View方法

  UIView *v3 = [[UIViewController alloc] initWithNibName:
@"View3" bundle:nil].view;

注:
当新建AViewController类时默认生成
1.AViewController.h 和 AViewController.m文件
2.AViewController.xib
即可 [[AViewController alloc] initWithNibName:@"" bundle:nil]

5).有xib( .h 和.m ) 文件,设置了File’s Owner
1.有View3.xib文件 View3.h View3.m
2.File’s Owner(File’s Owner可以设置为A.ViewContoller 或者 B.UIViewController)

其他controller使用代码:[UINib nibWithNibName:@"CycleScrollView"bundle:nil]

XIB拖XIB


一 .通过父View的Custom Class (XIB拖XIB) 有缺陷,不用

ViewController
加载xib时候会触发一些方法,如:

- (id)initWithCoder:(NSCoder *)aDecoder 
- (id)awakeAfterUsingCoder:(NSCoder*)aDecoder 

可以在-awakeAfterUsingCoder:(NSCoder*)aDecoder里通过 nibName 获取xib实例并加以替换

1.新建CustomView.h
CustomView.m
CustomView.xib

2.设置
CustomView.xib里父View
的Custom Class为
CustomView 设置其他Xib或StoryBoard里
View的 Custom Class为CustomView

3.在 CustomView
.m 文件里附上代码
- (id) awakeAfterUsingCoder:(NSCoder)aDecoder {
BOOL theThingThatGotLoadedWasJustAPlaceholder = ([[self subviews] count] == 0);
if (theThingThatGotLoadedWasJustAPlaceholder) {
SubView
theRealThing = [[self class] loadFromNibNoOwner];

        // pass properties through 
        [self copyUIPropertiesTo:theRealThing]; 
         
        //auto layout 
        self.translatesAutoresizingMaskIntoConstraints = NO; 
        theRealThing.translatesAutoresizingMaskIntoConstraints = NO; 
       
        return theRealThing; 
    } 
    return self; 
} 
 
-(void) copyUIPropertiesTo:(UIView *)view 
{ 
    // reflection did not work to get those lists, so I hardcoded them 
    // any suggestions are welcome here 
     
    NSArray *properties = 
    [NSArray arrayWithObjects: @"frame",@"bounds", @"center", @"transform", @"contentScaleFactor", @"multipleTouchEnabled", @"exclusiveTouch", @"autoresizesSubviews", @"autoresizingMask", @"clipsToBounds", @"backgroundColor", @"alpha", @"opaque", @"clearsContextBeforeDrawing", @"hidden", @"contentMode", @"contentStretch", nil]; 
     
    // some getters have 'is' prefix 
    NSArray *getters = 
    [NSArray arrayWithObjects: @"frame", @"bounds", @"center", @"transform", @"contentScaleFactor", @"isMultipleTouchEnabled", @"isExclusiveTouch", @"autoresizesSubviews", @"autoresizingMask", @"clipsToBounds", @"backgroundColor", @"alpha", @"isOpaque", @"clearsContextBeforeDrawing", @"isHidden", @"contentMode", @"contentStretch", nil]; 
     
    for (int i=0; i<[properties count]; i++) 
    { 
        NSString * propertyName = [properties objectAtIndex:i]; 
        NSString * getter = [getters objectAtIndex:i]; 
         
        SEL getPropertySelector = NSSelectorFromString(getter); 
         
        NSString *setterSelectorName = 
        [propertyName stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[propertyName substringToIndex:1] capitalizedString]]; 
         
        setterSelectorName = [NSString stringWithFormat:@"set%@:", setterSelectorName]; 
         
        SEL setPropertySelector = NSSelectorFromString(setterSelectorName); 
         
        if ([self respondsToSelector:getPropertySelector] && [view respondsToSelector:setPropertySelector]) 
        { 
            NSObject * propertyValue = [self valueForKey:propertyName]; 
             
            [view setValue:propertyValue forKey:propertyName]; 
        } 
    }     
} 

**二 .通过file’s owner的Custom Class (XIB拖XIB)
**
通过重载 initWithCoder方法来实现,因为通过 xib 来创建一个对象会调用到这个方法,所以我们需要在这个方法里做一些处理,把这个 CustomView的 xib中的内容加载进来,这时同样是需要通过代码来来加载

1.新建CustomView.h
CustomView.m
CustomView.xib

2.设置
CustomView.xib里file’s owner的Custom Class为 CustomView 设置其他Xib或StoryBoard里View的 Custom Class为CustomView
3.在 CustomView
.m 文件里附上代码
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
UIView *containerView = [[[UINib nibWithNibName:@"CustomView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
CGRect newFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
containerView.frame = newFrame;
[self addSubview:containerView];
}
return self;
}

推荐阅读更多精彩内容