XIB總結(jié)(代碼加載xib或xib拖xib)

view.xib的說明
View的custom cass是關聯(lián)自身的 File’s owner是關聯(lián)任意類的

方式一.xib拖xib 用File’s owner
方式二.代碼加載霸株,不向某個控制器關聯(lián)控件 用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關聯(lián)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時候會觸發(fā)一些方法不脯,如:

- (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方法來實現(xiàn)攻谁,因為通過 xib 來創(chuàng)建一個對象會調(diào)用到這個方法,所以我們需要在這個方法里做一些處理鳍烁,把這個 CustomView的 xib中的內(nèi)容加載進來载绿,這時同樣是需要通過代碼來來加載

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市辙培,隨后出現(xiàn)的幾起案子蔑水,更是在濱河造成了極大的恐慌,老刑警劉巖虏冻,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肤粱,死亡現(xiàn)場離奇詭異,居然都是意外死亡厨相,警方通過查閱死者的電腦和手機领曼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛮穿,“玉大人庶骄,你說我怎么就攤上這事〖酰” “怎么了单刁?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長府适。 經(jīng)常有香客問我羔飞,道長,這世上最難降的妖魔是什么檐春? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任逻淌,我火速辦了婚禮,結(jié)果婚禮上疟暖,老公的妹妹穿的比我還像新娘卡儒。我一直安慰自己,他們只是感情好俐巴,可當我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布骨望。 她就那樣靜靜地躺著,像睡著了一般欣舵。 火紅的嫁衣襯著肌膚如雪擎鸠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天邻遏,我揣著相機與錄音糠亩,去河邊找鬼虐骑。 笑死准验,一個胖子當著我的面吹牛赎线,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播糊饱,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼垂寥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了另锋?” 一聲冷哼從身側(cè)響起滞项,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎夭坪,沒想到半個月后文判,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡室梅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年戏仓,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片亡鼠。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡赏殃,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出间涵,到底是詐尸還是另有隱情仁热,我是刑警寧澤,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布勾哩,位于F島的核電站抗蠢,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏思劳。R本人自食惡果不足惜迅矛,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望敢艰。 院中可真熱鬧诬乞,春花似錦、人聲如沸钠导。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽牡属。三九已至票堵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間逮栅,已是汗流浹背悴势。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工窗宇, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人特纤。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓军俊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親捧存。 傳聞我的和親對象是個殘疾皇子粪躬,可洞房花燭夜當晚...
    茶點故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內(nèi)容