首页 > 代码库 > IOS开发-UIDatePicker和UIPickerView(拾取器)

IOS开发-UIDatePicker和UIPickerView(拾取器)

外观及作用

                   

UIDatePicker和事件相关,用来设置时间,选择时间等。UIPickerView则是自定义的,可以用作下拉列表等


运行结果

本示例主要简示了这些拾取器的用法,效果图如下所示(改成Iphone4大小了):

        


构建概要

1.新建基本视图或者基于TabView的工程,如果是基本的工程,需要额外操作转换为TabView,方法为:打开Main.storyboard,选中视图,然后在Xcode的菜单中Edit->Ebeded in->Tab View即可转换为TabView视图,如下图所示(gif录制真是拙计)。


2.新建一个视图,用作Single的界面。方法:拉一个PickView控件,从TabViewController拉一条直线到新建的View选中View Controller选项把新建的视图链接到TabView里面去。


3.给Single视图新建控制类,该类需要实现UIPickerViewDelegate, UIPickerViewDataSource及其对应的方法。(Single共需要实现3个方法)

4.连接Single控制类和Single界面,点击视图界面,修改视图左上方的Controller属性中的类为Single控制类名。


5.略


主要代码

h文件

//
//  CombineViewController.h
//  PickerViewDemo
//
//  Created by God Lin on 14/12/3.
//  Copyright (c) 2014??? arbboter. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CombineViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
    IBOutlet UIPickerView* _pickerView;
    NSArray* _arrayTeam;
    NSArray* _arrayTeamer;
    NSDictionary* dicData;
}

@property (nonatomic, retain) NSDictionary* _dicData;
@property (nonatomic, retain) UIPickerView* _pickerView;
@property (nonatomic, retain) NSArray* _arrayTeam;
@property (nonatomic, retain) NSArray* _arrayTeamer;

-(IBAction)onClick:(id)sender;
@end

m文件

//
//  CombineViewController.m
//  PickerViewDemo
//
//  Created by God Lin on 14/12/3.
//  Copyright (c) 2014年 arbboter. All rights reserved.
//

#import "CombineViewController.h"

@interface CombineViewController ()

@end

@implementation CombineViewController

@synthesize _pickerView;
@synthesize _dicData;
@synthesize _arrayTeam;
@synthesize _arrayTeamer;


#pragma mark UIPickerViewDelegate的协议
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString* title = nil;
    // 返回行数
    switch (component) {
        case 0:
            title = [_arrayTeam objectAtIndex:row];
            break;
        case 1:
            title = [_arrayTeamer objectAtIndex:row];
            break;
        default:
            break;
    }
    return title;
}

-(NSArray*)getTeamer:(NSInteger)row
{
    // 模拟字典数据
    //[NSArray arrayWithObjects:@"红队", @"白队", @"绿队", @"蓝队", nil];
    NSArray* teamer = nil;
    switch (row)
    {
        case 0:
            teamer = [NSArray arrayWithObjects:@"红队1", @"红队2", @"红队3", nil];
            break;
        case 1:
            teamer = [NSArray arrayWithObjects:@"白队1", @"白队2", @"白队3", @"白队4", nil];
            break;
        case 2:
            teamer = [NSArray arrayWithObjects:@"绿队1", @"绿队2", @"绿队3",  @"绿队4", @"绿队5", @"绿队6", nil];
            break;
        case 3:
            teamer = [NSArray arrayWithObjects:@"蓝队1", @"蓝队2", @"蓝队3", @"蓝队4", @"蓝队5", @"蓝队6", @"蓝队7", @"蓝队8", nil];
            break;
        default:
            teamer = [NSArray arrayWithObjects:@"其他1", nil];
            break;
    }
    return teamer;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
            _arrayTeamer = [self getTeamer:row];
            [self._pickerView reloadComponent:1];
            break;
            
        default:
            break;
    }
}


#pragma mark UIPickerViewDataSource的协议
// 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
{
    NSInteger nRet = 0;
    // 返回行数
    switch (component) {
        case 0:
            nRet = [_arrayTeam count];
            break;
        case 1:
            nRet = [_arrayTeamer count];
            break;
        default:
            break;
    }
    return nRet;
}

-(IBAction)onClick:(id)sender
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSArray* team = [NSArray arrayWithObjects:@"红队", @"白队", @"绿队", @"蓝队", nil];
    
    NSArray* teamer = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
    _arrayTeam = team;
    _arrayTeamer = teamer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

代码工程链接

IOS开发-UIDatePicker和UIPickerView(拾取器)