storyboard裏面實現的通信錄如今 用界面替換代碼數組
新加入一個xib文件app
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 }
刪除添加監聽的第二種方式就是 連線 (要將控制器環境改成一致便可)dom
從xib窗體更改owner文件關聯的class文件名字與storyboard.h裏面繼承名字一致ide
要更改加載解析xib加載方法 裏面將owner屬性改成當前窗體:selfspa
1 // 0.加載RowView文件,建立Objects數組按順序包裝全部控件到數組中返回 xib = nib app打包時候自動生成xib文件 2 NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:self options:nil];
新建一項目code
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