使用在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];
 
 
//---------------------------------------------------------------------
相关文章
相关标签/搜索