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;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容