新风作浪博客学习(九)两个UIPickerView控件间的数据依赖 .

本篇实现功能是两个选取器的关联操作,滚动第一个滚轮第二个滚轮内容随着第一个的变化而变化,然后点击按钮触发一个动作;工程是在 代码实现UIPickerView 一文中基础上修改的,建工程就不多说,先把效果图贴出来:

[img]
[img]http://dl.iteye.com/upload/attachment/0079/5152/f6553d48-9d90-3644-b137-0e8ad48701a5.png[/img]
[/img]

[img]
[img]http://dl.iteye.com/upload/attachment/0079/5154/ebf8d277-46ae-3bec-99c7-06f0aaf0b313.png[/img]
[/img]

1.首先在工程中建一个songInfo.plist文件,储存数据,
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5156/0c26f817-054c-3c78-8406-d4c807cb6162.png[/img]
[/img]

添加的内容是:
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5158/b531961e-0cd6-30e7-ac3f-d4554d5f3454.png[/img]
[/img]


2.在ViewController.h定一个选取器pickerView对象,两个数组,存放选取器数据和一个字典,读取plist文件
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
{
//定义滑轮组建
UIPickerView *pickerView;
// 储存第一个选取器的的数据
NSArray *singerData;
// 储存第二个选取器
NSArray *singData;
// 读取plist文件数据
NSDictionary *pickerDictionary;

}

-(void) buttonPressed:(id)sender;

@end



3.在ViewController.m文件中ViewDidLoad完成初始化

首先定义两个宏定义,分别表示两个选取器的索引序号值,放在 #import "ViewController.h"后面

#define singerPickerView 0

#define singPickerView 1

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
// 指定Delegate
pickerView.delegate=self;
pickerView.dataSource=self;
// 显示选中框
pickerView.showsSelectionIndicator=YES;
[self.view addSubview:pickerView];
// 获取mainBundle
NSBundle *bundle = [NSBundle mainBundle];
// 获取songInfo.plist文件路径
NSURL *songInfo = [bundle URLForResource:@"songInfo" withExtension:@"plist"];
// 把plist文件里内容存入数组
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfURL:songInfo];
pickerDictionary=dic;
// 将字典里面的内容取出放到数组中
NSArray *components = [pickerDictionary allKeys];

//选取出第一个滚轮中的值
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];

singerData = sorted;

// 根据第一个滚轮中的值,选取第二个滚轮中的值
NSString *selectedState = [singerData objectAtIndex:0];
NSArray *array = [pickerDictionary objectForKey:selectedState];
singData=array;


// 添加按钮
CGRect frame = CGRectMake(120, 250, 80, 40);
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
selectButton.frame=frame;
[selectButton setTitle:@"SELECT" forState:UIControlStateNormal];

[selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:selectButton];

}



按钮事件设置 :
-(void) buttonPressed:(id)sender
{
// 获取选取器某一行索引值
NSInteger singerrow =[pickerView selectedRowInComponent:singerPickerView];
NSInteger singrow = [pickerView selectedRowInComponent:singPickerView];
// 将singerData数组中值取出
NSString *selectedsinger = [singerData objectAtIndex:singerrow];
NSString *selectedsing = [singData objectAtIndex:singrow];
NSString *message = [[NSString alloc] initWithFormat:@"你选择了%@的%@",selectedsinger,selectedsing];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];

}



4.关于两个协议的代理方法 :
#pragma mark -
#pragma mark Picker Date Source Methods

//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
// 返回几就有几个选取器
return 2;
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

if (component==singerPickerView) {
return [singerData count];
}

return [singData count];

}


#pragma mark Picker Delegate Methods

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component==singerPickerView) {
return [singerData objectAtIndex:row];
}

return [singData objectAtIndex:row];


}



-(void)pickerView:(UIPickerView *)pickerViewt didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// 如果选取的是第一个选取器
if (component == singerPickerView) {
// 得到第一个选取器的当前行
NSString *selectedState =[singerData objectAtIndex:row];

// 根据从pickerDictionary字典中取出的值,选择对应第二个中的值
NSArray *array = [pickerDictionary objectForKey:selectedState];
singData=array;
[pickerView selectRow:0 inComponent:singPickerView animated:YES];


// 重新装载第二个滚轮中的值
[pickerView reloadComponent:singPickerView];
}
}



//设置滚轮的宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == singerPickerView) {
return 120;
}
return 200;
}



[color=red]在这个方法中,-(void)pickerView:(UIPickerView *)pickerViewt didSelectRow:(NSInteger)row inComponent:(NSInteger)component ,我把(UIPickerView *)pickerView参数改成了(UIPickerView *)pickerViewt,因为我定义的pickerView对象和参数发生冲突,所以把参数给改了下;[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值