通讯录 xib文件和storyboard混合编程
storyboard里面实现的通讯录现在 用界面替换代码
新加入一个xib文件
1 #pragma mark 用xib创建一行并添加文字和头像 2 - (UIView *)createRowView 3 { 4 // 0.加载RowView文件,创建Objects数组按顺序包装所有控件到数组中返回 xib = nib app打包时候自动生成xib文件 5 NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:self options:nil]; 6 // 1.取出一行view 7 UIView *rowViews = views[0]; 8 // 2.设置头像 9 UIButton *imgBtn = (UIButton *)[rowViews viewWithTag:1]; 10 NSString *names = [NSString stringWithFormat:@"0%d.jpg",arc4random_uniform(9)]; 11 // 2.1设置头像按钮背景图 随机获取头像 12 [imgBtn setImage:[UIImage imageNamed:names] forState:UIControlStateNormal]; 13 // 2.2添加头像点击监听 14 [imgBtn addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside]; 15 // 3.设置描述 16 UILabel *lab = (UILabel *)[rowViews viewWithTag:2]; 17 // 3.1随机获取描述信息 18 lab.text =_allNames[arc4random_uniform((int)_allNames.count)]; 19 // 4.设置删除按钮 20 UIButton *delBtn = (UIButton *)[rowViews viewWithTag:3]; 21 // 4.1添加删除点击监听 22 [delBtn addTarget:self action:@selector(delClick:) forControlEvents:UIControlEventTouchUpInside]; 23 return rowViews; 24 }
删除添加监听的第二种方式就是 连线 (要将控制器环境改为一致即可)
从xib窗体更改owner文件关联的class文件名字与storyboard.h里面继承名字一致
要更改加载解析xib加载方法 里面将owner属性改为当前窗体:self
1 // 0.加载RowView文件,创建Objects数组按顺序包装所有控件到数组中返回 xib = nib app打包时候自动生成xib文件 2 NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:self options:nil];
新建一项目
1 // 2 // ViewController.h 3 // 09-xib的ower 4 // 5 // Created by zjj on 15/5/11. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface ViewController : UIViewController 12 13 - (IBAction)leftClick:(UIButton *)sender; 14 - (IBAction)rightClick:(UIButton *)sender; 15 @end
1 // 2 // ViewController.m 3 // 09-xib的ower 4 // 5 // Created by zjj on 15/5/11. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 NSArray *rowArray = [[NSBundle mainBundle]loadNibNamed:@"myOwer" owner:self options:nil]; 20 [self.view addSubview:rowArray[0]];//连线按钮后自动监听方法 21 // xib好处 小众格式固定局部界面 22 // UIButton *btnZuo = (UIButton *)[rowViews viewWithTag:10]; 23 // [btnZuo addTarget:self action:@selector(leftClick:) forControlEvents:UIControlEventTouchUpInside]; 24 // 25 // UIButton *btnYou = (UIButton *)[rowViews viewWithTag:20]; 26 // [btnYou addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside ]; 27 28 } 29 30 - (void)leftClick:(UIButton *)sender 31 { 32 NSLog(@"左侧按钮点击"); 33 } 34 - (void)rightClick:(UIButton *)sender 35 { 36 NSLog(@"右侧按钮点击"); 37 } 38 @end