首先来看下一下效果图,如下:
在这里,使用到了系统自带的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];
|
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
;
|
以上是添加日期选择器的实现。
同理 ,地区联动也是一样的实现。不过相对比较复杂。这里不做详细介绍了。直接贴出实现部分。
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
;
}
|
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
{
return
3
;
// 返回3 表明什么呢?这个如果你不知道,你就别干了、、、表明该控件只包含3列,3列也就够了啊
}
// UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件指定列包含多少哥列表项
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
// 如果是第一列,返回provinces的个数
// 也就是provinces包含多少个元素,大天朝有多少个省份里面就有多少个
if
(component ==
0
) {
return
provinces.count;
}
else
if
(component ==
1
){
// 如果是第二列,返回cities的个数
return
cities.count;
}
else
{
return
areas.count;
}
}
// UIPickerViewDelegate中定义的方法,该方法返回NSString将作为UIPickerView中指定列和列表项上显示的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// 如果是第一列,返回provinces中row索引出得元素
switch
(component) {
case
0
:
return
[[provinces objectAtIndex:row] objectForKey:
@state
];
break
;
case
1
:
return
[[cities objectAtIndex:row] objectForKey:
@city
];
break
;
case
2
:
return
[areas objectAtIndex:row];
break
;
default
:
return
@;
break
;
}
}
// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (
void
)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch
(component) {
case
0
:
cities = [[provinces objectAtIndex:row] objectForKey:
@cities
];
[self.areaPicker selectRow:
0
inComponent:
1
animated:YES];
[self.areaPicker reloadComponent:
1
];
areas = [[cities objectAtIndex:
0
] objectForKey:
@areas
];
[self.areaPicker selectRow:
0
inComponent:
2
animated: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
;
case
1
:
areas = [[cities objectAtIndex:row] objectForKey:
@areas
];
[self.areaPicker selectRow:
0
inComponent:
2
animated:YES];
[self.areaPicker reloadComponent:
2
];
city = [[cities objectAtIndex:row] objectForKey:
@city
];
if
([areas count] >
0
) {
district = [areas objectAtIndex:
0
];
}
else
{
district = @;
}
break
;
case
2
:
if
([areas count] >
0
) {
district = [areas objectAtIndex:row];
}
else
{
district = @;
}
break
;
}
}
|