1创建MyView 继承于UIView
2创建UserInterface->View->Next->Save As 保存为MyView
3设置xib的Custom Class 为MyView 拖拽一个按钮到MyView成为属性btn1
4在UIViewController里面使用这个xib
具体代码是
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyView *view = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
view.frame = CGRectMake(0, 20, 320, 150);
[self.view addSubview:view];
MyView *view2 = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
view2.frame = CGRectMake(0, 180, 320, 150);
[self.view addSubview:view2];
[view2.btn1 setTitle:@"测试" forState:UIControlStateNormal];
}