使用在storyBoard以外的xib建立對象

一、在storyBoard以外的xib
要注意的是:TableView的代理必定要設置爲FilesOwner
使用:
方式一:
直接建立對象以下,(若是要使用xib裏的控件,那麼就要將xib裏的控件做爲成員變量了)
GACityRegonController *gaRegonVC=[[GACityRegonController alloc]init];
 
 
注意在storyBoard中,使用storyBoard獲取對象的:
如:
GAViewController *vc= [self.storyboard instantiateViewControllerWithIdentifier:@"GAViewController"];
 
 
方式二:
從編譯好的文件中獲取(NSBundle中)
能夠定義本身的類方法
// ———————————ConstomUIView.h-----------------------------------------------
一、建立、並設置好xib
 
#import <UIKit/UIKit.h>
 
@interface ConstomUIView : UIView
//將xib的控件都做爲屬性
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *imageButton;
@property (weak, nonatomic) IBOutlet UILabel *upLable;
@property (weak, nonatomic) IBOutlet UILabel *title;
//定義一個類方法,返回類對象
+(id)view;
@end
 
二、實現方法
// ———————————ConstomUIView.m-----------------------------------------------
#import "ConstomUIView.h"
@implementation ConstomUIView

+(id)view{
    //從NSBundle中獲取文件,建立類對象 
    return [[[NSBundle mainBundle]loadNibNamed:@"ConstomView" owner:nil options:nil] lastObject];
}
//防止橫屏控件拉伸
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
//不自動設置大小
        self.autoresizingMask = UIViewAutoresizingNone;
    }
    return self;
}
@end
 
三、使用:調用類方法建立對象
ConstomUIView *cityView=[ConstomUIView view];
 
 
//---------------------------------------------------------------------
相關文章
相關標籤/搜索