iPhone开发【六】常用控件之Picker View

转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8112404 作者:张燕广

实现的功能:演示Picker View的使用方法

关键词:Picker View

1、创建一个Single View Application工程,命名为:PickersDemo,如下图

iPhone开发【六】常用控件之Picker View_第1张图片

2、修改ViewController.xib,添加一个Picker View和一个Button控件,如下

iPhone开发【六】常用控件之Picker View_第2张图片

连接输出口、操作,如下

iPhone开发【六】常用控件之Picker View_第3张图片

3、视图控制器ViewController,需要实现协议UIPickerViewDataSource、UIPickerViewDelegate中的必须实现的方法,

在工程目录依次展开Frameworks->UIKit.framework->Headers,然后打开UIPickerView.h,在文件的尾部可以看到协议UIPickerViewDataSource、UIPickerViewDelegate的定义,如下

iPhone开发【六】常用控件之Picker View_第4张图片

4、修改ViewController.h:

[cpp]  view plain copy
  1. //  
  2. //  ViewController.h  
  3. //  PickersDemo  
  4. //  
  5. //  Created by Zhang Yanguang on 12-10-25.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #define kDeviceCategory 0  
  11. #define kDeviceName 1  
  12.   
  13. @interface ViewController : UIViewController  
  14.   
  15. @property(strong,nonatomic)IBOutlet UIPickerView *picker;  
  16. @property(nonatomic,retain)NSDictionary *appleDevices;  
  17. @property(nonatomic,retain)NSArray *deviceCategory;  
  18. @property(nonatomic,retain)NSArray *deviceName;  
  19.   
  20. -(IBAction)buttonPressed:(id)sender;  
  21. @end  

5、修改ViewController.m:

[cpp]  view plain copy
  1. //  
  2. //  ViewController.m  
  3. //  PickersDemo  
  4. //  
  5. //  Created by Zhang Yanguang on 12-10-25.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16. @synthesize picker;  
  17. @synthesize appleDevices;  
  18. @synthesize deviceCategory;  
  19. @synthesize deviceName;  
  20.   
  21. - (void)viewDidLoad  
  22. {  
  23.     [super viewDidLoad];  
  24.     // Do any additional setup after loading the view, typically from a nib.  
  25.     NSArray *array1 = [NSArray arrayWithObjects:@"iPhone",@"iPad",@"iPod",nil];  
  26.     NSArray *array2 = [NSArray arrayWithObjects:@"Mac",@"iMac",@"Mac Mini",@"Mac Pro",nil];  
  27.     NSDictionary  *dictionary= [NSDictionary dictionaryWithObjectsAndKeys:array1,@"Mobile",array2,@"Computers",nil];//注意用nil结束  
  28.     appleDevices = [[NSDictionary alloc]initWithDictionary:dictionary copyItems:YES];  
  29.       
  30.     NSArray *components = [self.appleDevices allKeys];  
  31.     NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];  
  32.     self.deviceCategory = sorted;  
  33.       
  34.     NSString *selectedCategory = [self.deviceCategory objectAtIndex:0];  
  35.     self.deviceName = [self.appleDevices objectForKey:selectedCategory];  
  36. }  
  37.   
  38. - (void)viewDidUnload  
  39. {  
  40.     [super viewDidUnload];  
  41.     picker = nil;  
  42.     appleDevices = nil;  
  43.     deviceCategory = nil;  
  44.     deviceName = nil;  
  45.     // Release any retained subviews of the main view.  
  46. }  
  47.   
  48. -(void)dealloc{  
  49.     [picker release];  
  50.     [appleDevices release];  
  51.     [deviceCategory release];  
  52.     [deviceName release];  
  53. }  
  54.   
  55. -(IBAction)buttonPressed:(id)sender{  
  56.     NSString *selectedCategory = [self.deviceCategory objectAtIndex:[self.picker selectedRowInComponent:kDeviceCategory]];  
  57.     NSString *selectedDevice = [self.deviceName objectAtIndex:[self.picker selectedRowInComponent:kDeviceName]];  
  58.     //NSLog(@"%@-%@",selectedCategory,selectedDevice);  
  59.   
  60.     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"结果" message:[NSString stringWithFormat:@"%@:%@",selectedCategory,selectedDevice]  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
  61.     [alertView show];  
  62. }  
  63.   
  64. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  65. {  
  66.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  67. }  
  68.   
  69. #pragma mark Picker View  DataSource methods  
  70. // returns the number of 'columns' to display.  
  71. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{  
  72.     return 2;  
  73. }  
  74.   
  75. // returns the # of rows in each component..  
  76. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{  
  77.     if(component == kDeviceCategory){  
  78.         return [self.deviceCategory count];  
  79.     }else{  
  80.         return [self.deviceName count];  
  81.     }  
  82. }  
  83.   
  84. #pragma mark Picker View  Delegate methods  
  85. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{  
  86.     if(component == kDeviceCategory){  
  87.         return [self.deviceCategory objectAtIndex:row];  
  88.     }else{  
  89.         return [self.deviceName objectAtIndex:row];  
  90.     }  
  91. }  
  92.   
  93. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{  
  94.     if(component == kDeviceCategory){  
  95.         NSString *selectedCategory = [self.deviceCategory objectAtIndex:row];  
  96.         NSArray *array = [self.appleDevices objectForKey:selectedCategory];  
  97.         self.deviceName = array;  
  98.           
  99.         [self.picker selectRow:0 inComponent:kDeviceName animated:YES];  
  100.         [self.picker reloadComponent:kDeviceName];  
  101.     }  
  102. }  
  103.   
  104. @end  

6、编译、运行,效果如下:

iPhone开发【六】常用控件之Picker View_第5张图片iPhone开发【六】常用控件之Picker View_第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



你可能感兴趣的:(iPhone开发【六】常用控件之Picker View)