博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS 之PickView
阅读量:4518 次
发布时间:2019-06-08

本文共 2731 字,大约阅读时间需要 9 分钟。

.h

#import 
@interface YYWViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIPickerView *pickerView;@property(retain,nonatomic)IBOutlet UILabel *selectLabel;@property (strong, nonatomic) NSArray *pickerData;@end

.m

#import "YYWViewController.h"@interface YYWViewController ()@end@implementation YYWViewController@synthesize pickerView;@synthesize selectLabel;-(void) addPickerView {    NSArray *array = [[NSArray alloc] initWithObjects:@"",@"Luke", @"Leia",                      @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];    _pickerData = array;    if (pickerView == nil) {        pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 240, 320, 460)];        pickerView.delegate = self;        pickerView.dataSource =self;        pickerView.showsSelectionIndicator = YES;        [self.view addSubview:pickerView];        [pickerView release];    }    }  -(void) addLabel {    if (selectLabel == nil) {        selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 40)];        selectLabel.text = @"";        selectLabel.font = [UIFont systemFontOfSize:12];        selectLabel.textAlignment = UITextAlignmentCenter;        selectLabel.backgroundColor = [UIColor grayColor];        [self.view addSubview:selectLabel];        [selectLabel release];            }    }// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad {    [super viewDidLoad];    [self addPickerView];    [self addLabel];    }#pragma mark pickerView delegate//返回pickerview的组件数- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {    return 1;}//返回每个组件上的行数- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {        return [_pickerData count];}//设置每行显示的内容- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {     return [_pickerData objectAtIndex:row];    }//当你选中pickerview的某行时会调用该函数。- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {    NSLog(@"You select row %d",row);        selectLabel.text = [NSString stringWithFormat:@"you select %@",[_pickerData  objectAtIndex:row]];}//设置指定组件上每行的宽度/* -(void)pickerView:(UIPickerView *)thePickerView widthForComponet:component {  }  */- (void)didReceiveMemoryWarning {    // Releases the view if it doesn't have a superview.    [super didReceiveMemoryWarning];        // Release any cached data, images, etc that aren't in use.}- (void)dealloc {        [pickerView release];    [super dealloc];}@end

 

转载于:https://www.cnblogs.com/Peter-Youny/archive/2013/04/23/3037344.html

你可能感兴趣的文章
第四章小结
查看>>
Windows7下python2.7.6环境变量配置
查看>>
java设计模式------代理模式
查看>>
WPF学习笔记----注释标记,使用自定义资源字典(style)文件的流程
查看>>
元素定位的八大法则
查看>>
Sublime Text 3 使用小记
查看>>
总结Pycharm里面常用的快捷键
查看>>
util.promisify 的那些事儿
查看>>
配置phpstudy+phpstorm+xdebug环境
查看>>
BZOJ 1079 [SCOI2008]着色方案
查看>>
[Win8.1系统]双系统
查看>>
HDU 3899 树形DP
查看>>
获取当前页面url信息
查看>>
Java容器类源码分析前言之集合框架结构(基于JDK8)
查看>>
linux下C/C++程序的内存布局
查看>>
单词计数问题
查看>>
php 魔术方法 __autoload()
查看>>
js div拖动动画运行轨迹效果
查看>>
使用Struts 2框架实现文件下载
查看>>
Recipe 1.9. Processing a String One Word at a Time
查看>>