WEB开发网
开发学院手机开发iPhone 开发 iPhone应用开发-UIPickerView选取器详解 阅读

iPhone应用开发-UIPickerView选取器详解

 2012-09-07 12:22:08 来源:WEB开发网   
核心提示:4、单击ViewController.m,向其中添加代码:4.1 在@implementation下一行添加代码:@synthesize picker; @synthesize provinceCities; @synthesize provinces; @synthesize cities;4.2 在ViewDidL

4、单击ViewController.m,向其中添加代码:

4.1 在@implementation下一行添加代码:

@synthesize picker; @synthesize provinceCities; @synthesize provinces; @synthesize cities;

4.2 在ViewDidLoad方法中添加代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSBundle *bundle = [NSBundle mainBundle]; NSURL *plistURL = [bundle URLForResource:@"provinceCities" withExtension:@"plist"]; NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL]; self.provinceCities = dictionary; NSArray *components = [self.provinceCities allKeys]; NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)]; self.provinces = sorted; NSString *selectedState = [self.provinces objectAtIndex:0]; NSArray *array = [provinceCities objectForKey:selectedState]; self.cities = array; }

代码中 NSBundle *bundle = [NSBundle mainBundle];

用于获得当前程序的Main Bundle,这个Bundle可以看成是一个文件夹,其中的内容遵循特定的框架。Main Bundle的一种主要用途是使用程序中的资源,如图片、声音等,本例中使用的是plist文件。下面的一行 NSURL *plistURL = [bundle URLForResource:@"provinceCities" withExtension:@"plist"];

用来获取provinceCities.plist的路径,之后将这个文件中的内容都放在一个NSDictionary对象中,用的是 NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];

4.3 找到viewDidUnload方法,添加代码: - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.picker = nil; self.provinceCities = nil; self.provinces = nil; self.cities = nil; }

4.4 在@end之前添加代码,实现buttonPressed方法: - (IBAction)buttonPressed:(id)sender { NSInteger provinceRow = [picker selectedRowInComponent:kProvinceComponent]; NSInteger cityRow = [picker selectedRowInComponent:kCityComponent]; NSString *province = [self.provinces objectAtIndex:provinceRow]; NSString *city = [self.cities objectAtIndex:cityRow]; NSString *title = [[NSString alloc] initWithFormat:@"你选择了%@.", city]; NSString *message = [[NSString alloc] initWithFormat:@"%@属于%@", city, province]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil]; [alert show]; }

4.5 在@end之前添加代码: #pragma mark - #pragma mark Picker Date Source Methods - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == kProvinceComponent) { return [self.provinces count]; } return [self.cities count]; } #pragma mark Picker Delegate Methods - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == kProvinceComponent) { return [self.provinces objectAtIndex:row]; } return [self.cities objectAtIndex:row]; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == kProvinceComponent) { NSString *selectedState = [self.provinces objectAtIndex:row]; NSArray *array = [provinceCities objectForKey:selectedState]; self.cities = array; [picker selectRow:0 inComponent:kCityComponent animated:YES]; [picker reloadComponent:kCityComponent]; } } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { if (component == kCityComponent) { return 150; } return 140; }

可以看到,跟上篇文章的例子相比,大部分代码是一样的,不同的是增加了pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component这个方法。这个方法中,当检测到修改的是左边转盘的值,则将self.cities中的内容替换成相应的数组,并执行[picker reloadComponent:kCityComponent];这个语句。

最后一个方法
(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

可以用来修改每个转盘的宽度,虽然在这个例子中不必要,但是我们得知道是怎么做的。

代码部分结束,接下来是使用Interface Builder添加控件、创建映射。
5、单击ViewController.xib,往其中添加一个UIPickerView控件和一个Button,按钮的名称改为“选择”,具体方法参照前面一篇文章:
接下来要做的就是拉几条线。
6、选中新添加的UIPickerView控件,按住Control,拖到File’s Owner图标,在弹出菜单选择delegate和dataSource:
打开Assistant Editor,确保其中打开的是ViewController.h,然后从picker属性前边的小圆圈拉线到UIPickerView控件:
同样,从buttonPressed方法前边的小圆圈拉线到“选择”按钮。
7、运行:

上一页  1 2 3 

Tags:iPhone 应用开发 UIPickerView

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接