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:

//
//  ViewController.h
//  PickersDemo
//
//  Created by Zhang Yanguang on 12-10-25.
//  Copyright (c) 2012年 MyCompanyName. All rights reserved.
//

#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:

//
//  ViewController.m
//  PickersDemo
//
//  Created by Zhang Yanguang on 12-10-25.
//  Copyright (c) 2012年 MyCompanyName. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize picker;
@synthesize appleDevices;
@synthesize deviceCategory;
@synthesize deviceName;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    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];//注意用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;
    // Release any retained subviews of the main view.
}

-(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]];
    //NSLog(@"%@-%@",selectedCategory,selectedDevice);

    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
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

// returns the # of rows in each component..
- (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、编译、运行,效果如下:

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


点击下载本文源代码

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