HardBirch

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

时间:12-10-25 栏目:iOS移动应用开发技术 作者:张飞不张,文采横飞 评论:0 点击: 2,080 次

实现的功能:演示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:

 

//
//  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

iPhone开发【六】常用控件之Picker View:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐