码迷,mamicode.com
首页 > 其他好文 > 详细

093向选择器中添加UIView子类

时间:2015-06-15 23:35:41      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
4 @property (strong, nonatomic) UIPickerView *pikVCustom;
5 @property (strong, nonatomic) NSArray *arrAnimal;
6 
7 @end

ViewController.m

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 - (void)layoutUI;
 5 - (void)buttonDidPush:(UIButton *)sender;
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     
13     [self layoutUI];
14 }
15 
16 - (void)didReceiveMemoryWarning {
17     [super didReceiveMemoryWarning];
18     // Dispose of any resources that can be recreated.
19 }
20 
21 - (void)viewWillAppear:(BOOL)animated {
22     [super viewWillAppear:animated];
23     [self.navigationController setNavigationBarHidden:NO animated:animated];
24     [self.navigationController setToolbarHidden:NO animated:animated];
25 }
26 
27 - (void)layoutUI {
28     self.view.backgroundColor = [UIColor whiteColor];
29     self.navigationItem.title = @"向选择器中添加UIView子类";
30     
31     //初始化动物图片数组(数据源)_arrAnimal
32     UIImage *imgDog = [UIImage imageNamed:@"Dog"];
33     UIImage *imgElephant = [UIImage imageNamed:@"Elephant"];
34     UIImage *imgLion = [UIImage imageNamed:@"Lion"];
35     UIImage *imgMonkey = [UIImage imageNamed:@"Monkey"];
36     _arrAnimal = @[imgDog, imgElephant, imgLion, imgMonkey];
37     _arrAnimal = @[_arrAnimal, _arrAnimal, _arrAnimal];
38     
39     _pikVCustom = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
40     CGPoint newPoint = self.view.center;
41     _pikVCustom.center = newPoint;
42     _pikVCustom.dataSource = self;
43     _pikVCustom.delegate = self;
44     [self.view addSubview:_pikVCustom];
45     
46     UIButton *btnChoice = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
47     newPoint.y += 160;
48     btnChoice.center = newPoint;
49     btnChoice.layer.masksToBounds = YES;
50     btnChoice.layer.cornerRadius = 10.0;
51     btnChoice.layer.borderColor = [UIColor blackColor].CGColor;
52     btnChoice.layer.borderWidth = 2.0;
53     [btnChoice setTitle:@"选择完毕" forState:UIControlStateNormal];
54     [btnChoice setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
55     [btnChoice addTarget:self
56                   action:@selector(buttonDidPush:)
57         forControlEvents:UIControlEventTouchUpInside];
58     [self.view addSubview:btnChoice];
59 }
60 
61 - (void)buttonDidPush:(UIButton *)sender {
62     NSInteger numberOfComponents = [_pikVCustom numberOfComponents];
63     NSMutableArray *mArrNew = [[NSMutableArray alloc] initWithCapacity:numberOfComponents];
64     for (NSInteger i=0; i<numberOfComponents; i++) {
65         UIImageView *imgV = (UIImageView *)[_pikVCustom viewForRow:[_pikVCustom selectedRowInComponent:i] forComponent:i];
66         UIImageView *imgVNew = [[UIImageView alloc] initWithImage:imgV.image];
67         UIBarButtonItem *barBtnNew = [[UIBarButtonItem alloc] initWithCustomView:imgVNew];
68         [mArrNew addObject:barBtnNew];
69     }
70     [self setToolbarItems:mArrNew animated:YES];
71 }
72 
73 #pragma mark - PickerView
74 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
75     return [_arrAnimal count]; //返回组件列数
76 }
77 
78 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
79     return [_arrAnimal[0] count]; //返回组件行数
80 }
81 
82 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
83     //如果参数view对象已经初始化,则直接显示(重用)
84     UIImageView *imgV = (UIImageView *)view;
85     if (!imgV) {
86         UIImage *img = _arrAnimal[component][row];
87         imgV = [[UIImageView alloc] initWithImage:img];
88     }
89     return imgV;
90 }
91 
92 @end

AppDelegate.h

1 #import <UIKit/UIKit.h>
2 
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 @property (strong, nonatomic) UIWindow *window;
5 @property (strong, nonatomic) UINavigationController *navigationController;
6 
7 @end

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 
 4 @interface AppDelegate ()
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
11     ViewController *viewController = [[ViewController alloc] init];
12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
13     _window.rootViewController = _navigationController;
14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
15     [_window makeKeyAndVisible];
16     return YES;
17 }
18 
19 - (void)applicationWillResignActive:(UIApplication *)application {
20 }
21 
22 - (void)applicationDidEnterBackground:(UIApplication *)application {
23 }
24 
25 - (void)applicationWillEnterForeground:(UIApplication *)application {
26 }
27 
28 - (void)applicationDidBecomeActive:(UIApplication *)application {
29 }
30 
31 - (void)applicationWillTerminate:(UIApplication *)application {
32 }
33 
34 @end

 

093向选择器中添加UIView子类

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4579316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!