iPhone开发UI组件UIDatePickerView的预习

2024-08-28 16:32

本文主要是介绍iPhone开发UI组件UIDatePickerView的预习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 新建工程如下:

编辑控制器的.h文件如下所示:

//
//  ViewController.h
//  UIDatePickerVIew
//
//  Created by apple on 15/9/2.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>
{int a;int b;int t;
}@property (nonatomic, strong) UIPickerView *pickView;@property (nonatomic, strong) NSMutableArray *pickerNameArray;
@property (nonatomic, strong) NSMutableArray *pickerPlaceArray;
@property (nonatomic, strong) NSMutableDictionary *typeDictionary;
@property (nonatomic, strong) UILabel * lblSelect;
@property (nonatomic, strong) NSString *selectStr;@property (nonatomic, strong) NSTimer * timer;
@property (nonatomic, strong) UIButton *btn;@property (nonatomic, strong) UIImageView *zhen;
@property (nonatomic, strong) UIImageView *pan;@end
编辑控制器的.m文件如下所示:

//
//  ViewController.m
//  UIDatePickerVIew
//
//  Created by apple on 15/9/2.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//#import "ViewController.h"
#define WIDTH    [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height@interface ViewController ()
@property (nonatomic, strong) NSString *str1;
@property (nonatomic, strong) NSString *str2;
@property (nonatomic, strong) NSDate *start;
@property (nonatomic, strong) NSDate *end;
@end@implementation ViewController- (void)viewDidLoad
{[super viewDidLoad];[self assignForPickView];self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(10, WIDTH/2+50, WIDTH-20, WIDTH/3)];self.pickView.backgroundColor = [UIColor yellowColor];self.pickView.dataSource = self;self.pickView.delegate = self;self.pickView.showsSelectionIndicator = YES;[self.view addSubview:self.pickView];// 显示数据self.lblSelect = [[UILabel alloc ] initWithFrame:CGRectMake(0, 10, WIDTH, WIDTH/6)];self.lblSelect .text = self.selectStr;self.lblSelect.textAlignment = NSTextAlignmentCenter;[self.view addSubview:self.lblSelect];// buttonself.btn = [[UIButton alloc] initWithFrame:CGRectMake(10, WIDTH/2+WIDTH/3+100, WIDTH-20, WIDTH/5)];[self.btn  setTitle:@"点击抽奖" forState:UIControlStateNormal];[self.btn setTitle:@"开始抽奖" forState:UIControlStateHighlighted];[self.btn addTarget:self action:@selector(chouJiang) forControlEvents:UIControlEventTouchUpInside];[self.btn setBackgroundColor:[UIColor redColor]];[self.view addSubview:self.btn];// 显示表盘self.pan = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (WIDTH-40)/2, (WIDTH-40)/2)];self.pan.center = CGPointMake(WIDTH/2, 120);self.pan.image = [UIImage imageNamed:@"1.png"];[self.view addSubview:self.pan];// 显示表针self.zhen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 11, (WIDTH - 40)/4)];self.zhen.center = CGPointMake(WIDTH/2, 120);self.zhen.image = [UIImage imageNamed:@"3.png"];[self.view addSubview:self.zhen];[self.view bringSubviewToFront:self.lblSelect];}//抽奖监听方法
-(void)chouJiang
{self.start  = [[NSDate alloc] init];NSLog(@"t1= %d",t);self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(jiang) userInfo:nil repeats:YES];}-(void) jiang
{self.end = [[NSDate alloc] init];t = self.start.timeIntervalSince1970;int t2 = self.end.timeIntervalSince1970;NSLog(@"t2= %d  t1= %d",t2,t);if ((t2 - t) == 5 ) {[self.timer invalidate];}int b1 = arc4random()%100+100000;for (int i = 0; i<b1 ; i++) {self.zhen.transform = CGAffineTransformRotate(self.zhen.transform, -M_PI_4/180);}a = arc4random() % (self.pickerNameArray.count);NSMutableArray  *BB = [self.typeDictionary  objectForKey:self.pickerNameArray[a]];b = arc4random() % (BB.count);[self  pickerView:self.pickView didSelectRow:a inComponent:0];[self  pickerView:self.pickView didSelectRow:b inComponent:1];[self.pickView reloadComponent:0];[self.pickView  selectRow:a inComponent:0 animated:YES];}-(void) assignForPickView
{self.pickerNameArray =[ [NSMutableArray alloc] initWithObjects:@"河南",@"北京",@"山东",@"河北" ,nil];NSMutableArray *hArr = [NSMutableArray arrayWithObjects:@"周口", @"郑州",@"焦作",@"驻马店",nil];NSMutableArray *bArr = [NSMutableArray arrayWithObjects:@"昌平",@"海淀",@"朝阳",@"丰台",@"大兴", nil];NSMutableArray *sArr = [NSMutableArray arrayWithObjects:@"青岛",@"烟台", nil];NSMutableArray *hBArr = [NSMutableArray arrayWithObjects:@"石家庄", nil];NSMutableArray *typeArr = [NSMutableArray arrayWithObjects:hArr,bArr,sArr,hBArr, nil];self.typeDictionary = [NSMutableDictionary dictionaryWithObjects:typeArr forKeys:self.pickerNameArray];self.pickerPlaceArray = hArr;}// 分为多少列
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{return 2;  }// 每一列有多少行
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{NSInteger row = 0;switch (component) {case 0:row = self.pickerNameArray.count;break;case 1:row = self.pickerPlaceArray.count;break;}return row;
}//  利用系统自带的格式。
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{if (component == 0) {return self.pickerNameArray[row];}else{return self.pickerPlaceArray[row];}}// 自定义格式
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{UILabel *contentLabel = nil;if (component == 0) {contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];contentLabel.textAlignment = NSTextAlignmentCenter;contentLabel.text = self.pickerNameArray[row];contentLabel.font = [UIFont systemFontOfSize:14];contentLabel.backgroundColor = [UIColor cyanColor];}else{contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 30)];contentLabel.text = self.pickerPlaceArray[row];contentLabel.textAlignment = NSTextAlignmentCenter;contentLabel.font = [UIFont systemFontOfSize:14];contentLabel.backgroundColor = [UIColor darkGrayColor];}return contentLabel;
}-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{switch (component) {case 0:{self.str1 = self.pickerNameArray[row];self.pickerPlaceArray = self.typeDictionary[self.str1];if (self.pickerPlaceArray.count >1) {self.selectStr = [NSString stringWithFormat:@"%@, %@",self.str1,[self.pickerPlaceArray objectAtIndex:b]];}else{self.selectStr = self.str1;}// 更新视图[pickerView reloadComponent:1];[pickerView selectRow:b inComponent:1 animated:YES];}break;case 1 :{self.str2 = self.pickerPlaceArray[row];self.selectStr = [NSString stringWithFormat:@"%@ ,%@",self.str1,self.str2];break;}break;}self.lblSelect.text = self.selectStr;
}// 设置每一列的行高
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{return 40;
}// 设置每一列的宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{CGFloat width = 0.0;if (component == 0) {width = 100;}elsewidth = 180;return width;
}
- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
运行结果如下:




这篇关于iPhone开发UI组件UIDatePickerView的预习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115294

相关文章

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言

Python中的可视化设计与UI界面实现

《Python中的可视化设计与UI界面实现》本文介绍了如何使用Python创建用户界面(UI),包括使用Tkinter、PyQt、Kivy等库进行基本窗口、动态图表和动画效果的实现,通过示例代码,展示... 目录从像素到界面:python带你玩转UI设计示例:使用Tkinter创建一个简单的窗口绘图魔法:用

基于Qt Qml实现时间轴组件

《基于QtQml实现时间轴组件》时间轴组件是现代用户界面中常见的元素,用于按时间顺序展示事件,本文主要为大家详细介绍了如何使用Qml实现一个简单的时间轴组件,需要的可以参考下... 目录写在前面效果图组件概述实现细节1. 组件结构2. 属性定义3. 数据模型4. 事件项的添加和排序5. 事件项的渲染如何使用

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

element-ui下拉输入框+resetFields无法回显的问题解决

《element-ui下拉输入框+resetFields无法回显的问题解决》本文主要介绍了在使用ElementUI的下拉输入框时,点击重置按钮后输入框无法回显数据的问题,具有一定的参考价值,感兴趣的... 目录描述原因问题重现解决方案方法一方法二总结描述第一次进入页面,不做任何操作,点击重置按钮,再进行下

在 VSCode 中配置 C++ 开发环境的详细教程

《在VSCode中配置C++开发环境的详细教程》本文详细介绍了如何在VisualStudioCode(VSCode)中配置C++开发环境,包括安装必要的工具、配置编译器、设置调试环境等步骤,通... 目录如何在 VSCode 中配置 C++ 开发环境:详细教程1. 什么是 VSCode?2. 安装 VSCo

C#图表开发之Chart详解

《C#图表开发之Chart详解》C#中的Chart控件用于开发图表功能,具有Series和ChartArea两个重要属性,Series属性是SeriesCollection类型,包含多个Series对... 目录OverviChina编程ewSeries类总结OverviewC#中,开发图表功能的控件是Char

鸿蒙开发搭建flutter适配的开发环境

《鸿蒙开发搭建flutter适配的开发环境》文章详细介绍了在Windows系统上如何创建和运行鸿蒙Flutter项目,包括使用flutterdoctor检测环境、创建项目、编译HAP包以及在真机上运... 目录环境搭建创建运行项目打包项目总结环境搭建1.安装 DevEco Studio NEXT IDE