iPhone开发——Picker View

Home / iOS MrLee 2015-6-15 2653

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 
#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、编译、运行,效果如下:

本文链接:https://www.it72.com/3062.htm

推荐阅读
最新回复 (0)
返回
站点信息
  • 文章
    2237
  • 用户
    1311
  • 访客
    7881704
每日一句