转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8112404 作者:张燕广
实现的功能:演示Picker View的使用方法
关键词:Picker View
1、创建一个Single View Application工程,命名为:PickersDemo,如下图
2、修改ViewController.xib,添加一个Picker View和一个Button控件,如下
连接输出口、操作,如下
3、视图控制器ViewController,需要实现协议UIPickerViewDataSource、UIPickerViewDelegate中的必须实现的方法,
在工程目录依次展开Frameworks->UIKit.framework->Headers,然后打开UIPickerView.h,在文件的尾部可以看到协议UIPickerViewDataSource、UIPickerViewDelegate的定义,如下
4、修改ViewController.h:
-
-
-
-
-
-
-
-
- #import <UIKit/UIKit.h>
- #define kDeviceCategory 0
- #define kDeviceName 1
-
- @interface ViewController : UIViewController
-
- @property(strong,nonatomic)IBOutlet UIPickerView *picker;
- @property(nonatomic,retain)NSDictionary *appleDevices;
- @property(nonatomic,retain)NSArray *deviceCategory;
- @property(nonatomic,retain)NSArray *deviceName;
-
- -(IBAction)buttonPressed:(id)sender;
- @end
5、修改ViewController.m:
-
-
-
-
-
-
-
-
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @end
-
- @implementation ViewController
- @synthesize picker;
- @synthesize appleDevices;
- @synthesize deviceCategory;
- @synthesize deviceName;
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- NSArray *array1 = [NSArray arrayWithObjects:@"iPhone",@"iPad",@"iPod",nil];
- NSArray *array2 = [NSArray arrayWithObjects:@"Mac",@"iMac",@"Mac Mini",@"Mac Pro",nil];
- NSDictionary *dictionary= [NSDictionary dictionaryWithObjectsAndKeys:array1,@"Mobile",array2,@"Computers",nil];
- appleDevices = [[NSDictionary alloc]initWithDictionary:dictionary copyItems:YES];
-
- NSArray *components = [self.appleDevices allKeys];
- NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
- self.deviceCategory = sorted;
-
- NSString *selectedCategory = [self.deviceCategory objectAtIndex:0];
- self.deviceName = [self.appleDevices objectForKey:selectedCategory];
- }
-
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- picker = nil;
- appleDevices = nil;
- deviceCategory = nil;
- deviceName = nil;
-
- }
-
- -(void)dealloc{
- [picker release];
- [appleDevices release];
- [deviceCategory release];
- [deviceName release];
- }
-
- -(IBAction)buttonPressed:(id)sender{
- NSString *selectedCategory = [self.deviceCategory objectAtIndex:[self.picker selectedRowInComponent:kDeviceCategory]];
- NSString *selectedDevice = [self.deviceName objectAtIndex:[self.picker selectedRowInComponent:kDeviceName]];
-
-
- UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"结果" message:[NSString stringWithFormat:@"%@:%@",selectedCategory,selectedDevice] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [alertView show];
- }
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- }
-
- #pragma mark Picker View DataSource methods
-
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
- return 2;
- }
-
-
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
- if(component == kDeviceCategory){
- return [self.deviceCategory count];
- }else{
- return [self.deviceName count];
- }
- }
-
- #pragma mark Picker View Delegate methods
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
- if(component == kDeviceCategory){
- return [self.deviceCategory objectAtIndex:row];
- }else{
- return [self.deviceName objectAtIndex:row];
- }
- }
-
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
- if(component == kDeviceCategory){
- NSString *selectedCategory = [self.deviceCategory objectAtIndex:row];
- NSArray *array = [self.appleDevices objectForKey:selectedCategory];
- self.deviceName = array;
-
- [self.picker selectRow:0 inComponent:kDeviceName animated:YES];
- [self.picker reloadComponent:kDeviceName];
- }
- }
-
- @end
6、编译、运行,效果如下:
点击下载本文源代码
=========================================================================
以下是我实现的,更为简单的单选择器:
//
// MyTestViewController.m
// MyTest
//
// Created by H3c on 13-1-4.
// Copyright (c) 2013年.H3c All rights reserved.
//
#import "MyTestViewController.h"
@interface MyTestViewController ()
@end
@implementation MyTestViewController
@synthesize picker;
@synthesize appleDevices;
@synthesize deviceCategory;
@synthesize deviceName;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *arr1 = [NSArray arrayWithObjects:@"1",@"5",@"7",@"4", nil] ;
// 排序
NSArray *sorted = [arr1 sortedArrayUsingSelector:@selector(compare:)];
self.deviceCategory = sorted;
// 设定默认值
[self.picker selectRow:2 inComponent:0 animated:YES];
}
- (void)viewDidUnload
{
picker = nil;
appleDevices = nil;
deviceCategory = nil;
deviceName = nil;
[super viewDidUnload];
}
-(void)dealloc{
[picker release];
[appleDevices release];
[deviceCategory release];
[deviceName release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)click:(id)sender
{
// 获取选中内容
NSString *selectedCategory = [self.deviceCategory objectAtIndex:[self.picker selectedRowInComponent:0]];
UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"Note" message:selectedCategory delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
[alertV show];
}
// 列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// 行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.deviceCategory count];
}
// Cell内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.deviceCategory objectAtIndex:row];
}
@end
#import <UIKit/UIKit.h>
#define one 0;
@interface MyTestViewController : UIViewController<UIActionSheetDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
@property(strong,nonatomic)IBOutlet UIPickerView *picker;
@property(nonatomic,retain)NSDictionary *appleDevices;
@property(nonatomic,retain)NSArray *deviceCategory;
@property(nonatomic,retain)NSArray *deviceName;
- (IBAction)click:(id)sender;
@end