PickerView 的使用方法

本文详细介绍了如何在iOS应用中使用UIPickerView创建一个选择框,包括初始化、配置和响应用户操作的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  ViewController.m

//  testPickerView

//

//  Created by yuyezhulan on 14/11/9.

//  Copyright (c) 2014 yuyezhulan. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];

    //    指定Delegate

    pickerView.delegate=self;

    //    显示选中框

    pickerView.showsSelectionIndicator=YES;

    [self.view addSubview:pickerView];

    

    NSArray *dataArray = [[NSArray alloc]initWithObjects:@"许嵩",@"周杰伦",@"梁静茹",@"许飞",@"凤凰传奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永传", nil];

    

    pickerData=dataArray;

    

//    //     添加按钮

//    CGRect frame = CGRectMake(120, 250, 80, 40);

//    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//    selectButton.frame=frame;

//    [selectButton setTitle:@"SELECT" forState:UIControlStateNormal];

//    

//    [selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

//    

//    [self.view addSubview:selectButton];

    

}


-(void)buttonPressed:(id)sender{

    NSLog(@"Button Pressed!");

}



//返回显示的列数

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 5;

}

//返回当前列显示的行数

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    return [pickerData count];

}


#pragma mark Picker Delegate Methods


//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    return [pickerData objectAtIndex:row];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值