UIPickerView基本用法
#import "ViewController.h"
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
{
UILabel *lable;
NSArray *array;
}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
array=[NSArray arrayWithObjects:@"0",@"1",@"2",@"3" ,@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",nil];
UIButton *button3=[UIButton buttonWithType:UIButtonTypeCustom];
button3.backgroundColor=[UIColor greenColor];
button3.frame=CGRectMake(100, 300, 120, 50);
[button3 addTarget:self action:@selector(showMyPickerView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
}
-(void)showMyPickerView:(UIButton *)sender
{
//UIPickerView选择器的功能
UIView *whiteView=[[UIView alloc]initWithFrame:self.view.frame];
//添加视图进行遮挡
whiteView.tag=150;
whiteView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:whiteView];
//UIPickerView选择器的功能,实现数据的选择
UIPickerView *pickerView1=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 280, 300)];
pickerView1.center=whiteView.center;
pickerView1.delegate=self;
pickerView1.dataSource=self;
[whiteView addSubview:pickerView1];
UIButton *button0=[UIButton buttonWithType:UIButtonTypeCustom];
button0.frame=CGRectMake(0, 0, 80, 60);
button0.backgroundColor=[UIColor greenColor];
[button0 setTitle:@"close" forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(closePickerView:) forControlEvents:UIControlEventTouchUpInside];
[whiteView addSubview:button0];
lable=[[UILabel alloc]initWithFrame:CGRectMake(100,20, 200, 40)];
lable.backgroundColor=[UIColor yellowColor];
lable.tag=160;
[whiteView addSubview:lable];
}
-(void)closePickerView:(UIButton *)sender
{
UIView *removeView=[self.view viewWithTag:150];
[removeView removeFromSuperview];//移除白色遮挡视图
}
//返回选择器的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//返回当前显示的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return array.count;
}
//显示数组中的数字在对应的行中
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [array objectAtIndex:row];
}
//获取单元行的内容
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *str1=[array objectAtIndex:row];
NSString *string1=[self pickerView:pickerView titleForRow:row forComponent:0];
UILabel *getlable=(UILabel *)[self.view viewWithTag:160];
NSLog(@"%@---%@",str1,string1);
getlable.text=string1;
}
UIPickerView基本用法的更多相关文章
- iOS学习——UIPickerView的实现年月选择器
最近项目上需要用到一个选择器,选择器中的内容只有年和月,而在iOS系统自带的日期选择器UIDatePicker中却只有四个选项如下,分别是时间(时分秒).日期(年月日).日期+时间(年月日时分)以及倒 ...
- UIPickerView用法(左右比例,整体大小,字体大小)
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; pickerView.autoresizingM ...
- Swift - 选择框(UIPickerView)的用法
1,选择框可以让用户以滑动的方式选择值.示例如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- UIPickerView的使用(一)
简介:UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPick ...
- iOs基础篇(二十二)—— UIPickerView、UIDatePicker控件的使用
一.UIPickerView UIPickerView是一个选择器控件,可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活. 1.常用属性 (1)num ...
- ios开发 <;AppName>;-Prefix.pch文件的用法详解
我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...
- iOS开发——高级UI之OC篇&;UIdatePicker&;UIPickerView简单使用
UIdatePicker&UIPickerView简单使用 /***************************************************************** ...
- UIPickerView
1.UIPickView什么时候用? 通常在注册模块,当用户需要选择一些东西的时候,比如说城市,往往弹出一个PickerView给他们选择. UIPickView常见用法,演示实例程序 1> 独 ...
- UIPickerView(选择器)
UIPickerView也是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活. UIPicke ...
随机推荐
- 辗转相除法求最大公约数和最小公倍数【gcd】
要求最小公倍数可先求出最大公约数 设要求两个数a,b的最大公约数 伪代码: int yushu,a,b: while(b不等于0) { yushu=a对b求余 b的值赋给a yushu的值赋给b } ...
- php爬虫的两种思路
写php爬虫可能最大的问题就是php脚本执行时间的问题了,对于这个问题,我找到了两种解决方法. 第一种通过代码set_time_limit(0)或者ini_set("max_executio ...
- php的json_encode函数问题
php的json_encode函数问题: $ary = []; $ary[0] = 'a'; $ary[1] = 'b'; echo json_encode($ary) . '<br>'; ...
- 远程访问mysql(转)
GRANT ALL PRIVILEGES ON *.* TO '<username>'@'<remote addr or %(for all ip addr)>'IDENTIF ...
- android屏幕适配之度量单位、屏幕分类、图标尺寸归类分析
好久没有做android项目UI的适配了,好多基本概念都已经模糊了,于是萌生了将屏幕分辨率.常用单位.常用图标尺寸等信息规整的想法,一下就是通过查询资料,自己验证的一些随笔,如有失误之处,望大家及时予 ...
- NodeJS Stream 五:双工流
双工流就是同时实现了 Readable 和 Writable 的流,即可以作为上游生产数据,又可以作为下游消费数据,这样可以处于数据流动管道的中间部分,即 rs.pipe(rws1).pipe(rws ...
- js、jq事件绑定方式总结——以click事件为例
一.JavaScript点击事件绑定方法 1.1 HTML onclick事件属性 <button onclick="clickMe(this)">click me&l ...
- js forEach跳出循环
假设当我们只需知道某个数组有没有某个属性,如果找到了直接跳出循环,省略掉剩下的循环步骤是较优化的操作,但是for中是可以利用break跳出循环,但break在forEach中无效,那么forEach能 ...
- Struts学习总结-02 上传文件
Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的目录,以确保数据不丢失. ...
- c++ 如何获取多线程的返回值?(std::thread ,std::async)
//简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...