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如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

Spring组件初始化扩展点BeanPostProcessor的作用详解

《Spring组件初始化扩展点BeanPostProcessor的作用详解》本文通过实战案例和常见应用场景详细介绍了BeanPostProcessor的使用,并强调了其在Spring扩展中的重要性,感... 目录一、概述二、BeanPostProcessor的作用三、核心方法解析1、postProcessB