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实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录