在UIAcitionSheet中添加UIPickerView

来源:互联网 发布:centos 7 nfs 安装 编辑:程序博客网 时间:2024/06/11 09:53

首先来看下一下效果图,如下:

\\

在这里,使用到了系统自带的UIActionSheet,熟悉iOS开发的人应该都知道。UIActionSheet是一个底部弹出的选择按钮项控件,可以添加多项,并为每项添加点击事件。它的初始化代码为:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @
 
 
  
 
 
 
 ;
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                         delegate:self
                                                cancelButtonTitle:@取消
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@确定,nil];
我们可以看到这里初始化了一个NSString,然后根据屏幕的横竖屏选择使用三目运算符指定填充字符串。这里只用字符 换行符作为字符使得UIActionSheet的顶部出现一段空的布局以提供UIDatePicker和UIPickerView使用。

 

 

?
1
2
3
4
5
6
7
8
actionSheet.userInteractionEnabled = YES;
actionSheet.backgroundColor = [UIColor clearColor];
datePicker = [[UIDatePicker alloc] init];
datePicker.tag = 1000;
datePicker.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:datePicker];
[actionSheet showInView:self.view];
actionSheet.tag = 100;
上述代码设置UIActionSheet可交互,背景为白色,必须设置背景色,否则可能部分机型会出现 显示字符。然后初始化UIDatePicker并绑定tag以提供后面方法调用。紧接着设置日期格式,当然也可以是指日期时间格式。最好通过UIActionSheet的addSubView方法添加到之前我们用换行符占位的布局上,并且使用showInView显示在最上面,同样给UIActionSheet设置一个tag值标记。

 


以上是添加日期选择器的实现。
同理 ,地区联动也是一样的实现。不过相对比较复杂。这里不做详细介绍了。直接贴出实现部分。

plist文件就是普通的国内各省市,直辖市区县的信息,目前并不是很完善,本人地理知识有限,需要不断优化添加。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma mark - 调用地区联动方法
- (void)showAreaPickerView{
    // 加载plist文件,初始化三个NSArray对象,然后做一些非法的事情,你懂的
    provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@area.plist ofType:nil]];
    cities = [[provinces objectAtIndex:0] objectForKey:@cities];
    state = [[provinces objectAtIndex:0] objectForKey:@state];
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @
 
 
 
 
 
 
 
 
 : @
 
 
 
 
 
 
 
 
 
 
 
 ;
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                             delegate:self
                                                    cancelButtonTitle:@取消
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@确定,nil];
    actionSheet.userInteractionEnabled = YES;
    actionSheet.backgroundColor = [UIColor clearColor];
    areaPicker = [[UIPickerView alloc] init];
    areaPicker.dataSource = self;
    areaPicker.delegate = self;
     
    [actionSheet addSubview:areaPicker];
    [actionSheet showInView:self.view];
    actionSheet.tag = 200;
}
接下来是UIPickerViewDataSource数据源方法和UIPickerViewDelegate代理方法。部分地方仍然可以精简。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// UIPickerViewDataSource中定义的方法,该方法的返回值决定改控件包含多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return3;// 返回3 表明什么呢?这个如果你不知道,你就别干了、、、表明该控件只包含3列,3列也就够了啊
}
// UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件指定列包含多少哥列表项
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    // 如果是第一列,返回provinces的个数
    // 也就是provinces包含多少个元素,大天朝有多少个省份里面就有多少个
    if(component == 0) {
        returnprovinces.count;
    }elseif(component == 1){
        // 如果是第二列,返回cities的个数
        returncities.count;
    }else{
        returnareas.count;
    }
}
// UIPickerViewDelegate中定义的方法,该方法返回NSString将作为UIPickerView中指定列和列表项上显示的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // 如果是第一列,返回provinces中row索引出得元素
    switch(component) {
        case0:
            return[[provinces objectAtIndex:row] objectForKey:@state];
            break;
        case1:
            return[[cities objectAtIndex:row] objectForKey:@city];
            break;
        case2:
            return[areas objectAtIndex:row];
            break;
        default:
            return@;
            break;
    }
}
// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch(component) {
        case0:
            cities = [[provinces objectAtIndex:row] objectForKey:@cities];
            [self.areaPicker selectRow:0inComponent:1animated:YES];
            [self.areaPicker reloadComponent:1];
             
            areas = [[cities objectAtIndex:0] objectForKey:@areas];
            [self.areaPicker selectRow:0inComponent:2animated:YES];
            [self.areaPicker reloadComponent:2];
             
            state = [[provinces objectAtIndex:row] objectForKey:@state];
            city = [[cities objectAtIndex:0] objectForKey:@city];
            if([areas count] > 0) {
                district = [areas objectAtIndex:0];
            }else{
                district = @;
            }
            break;
        case1:
            areas = [[cities objectAtIndex:row] objectForKey:@areas];
            [self.areaPicker selectRow:0inComponent:2animated:YES];
            [self.areaPicker reloadComponent:2];
             
            city = [[cities objectAtIndex:row] objectForKey:@city];
            if([areas count] > 0) {
                district = [areas objectAtIndex:0];
            }else{
                district = @;
            }
            break;
        case2:
            if([areas count] > 0) {
                district = [areas objectAtIndex:row];
            }else{
                district = @;
            }
            break;
    }
}
具体逻辑实现可能有一些小细节处理的不好,但实际使用还是很棒的。这里就不贴动态gif图了。 本篇到此为止。
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在瓜子上买车复检有问题怎么办 更换车壳车架号怎么办 吸完甲醛的绿萝怎么办 如果公务员复检不合格有异议怎么办 国考公务员政审没有毕业证怎么办 打针硬块4年不消怎么办 外墙补起来难看不好卖怎么办 杠精现实中应该怎么办 发现记者报道假新闻怎么办 2018消防兵转制到期士官怎么办 小孩睡觉老想着军训怎么办 1岁宝宝太老实了怎么办 上课小孩很调皮不听话怎么办 初中学生上课爱说话调皮怎么办 8个月婴儿疝气怎么办 头部疤痕不长发怎么办呢 有纹身想去当兵怎么办 在部队干活的钱怎么办 新兵5公里超过标准时间怎么办 17个月婴儿裹手怎么办 7个月婴儿裹手怎么办 俩月孩子裹手怎么办 婴儿 3个月 裹手怎么办 一岁宝宝裹手怎么办 5个月宝宝裹手怎么办 不想让孩子裹手怎么办 四个月宝宝裹手怎么办 开车撞狗了跑了怎么办 母螃蟹抱卵了怎么办 我能怎么办我也想睡觉 一个人的微信群找不到了怎么办 一个人的微信被删了找不到了怎么办 不想回对方微信怎么办 喜欢我的人太多怎么办 假如战争今夜打响我们该怎么办 约客户客户说忙怎么办 第一天来姨妈痛怎么办 痛经痛的很厉害怎么办 工作第一天被辞怎么办 第一天练车紧张怎么办 到新公司第一天怎么办