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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

【区块链 + 人才服务】区块链集成开发平台 | FISCO BCOS应用案例

随着区块链技术的快速发展,越来越多的企业开始将其应用于实际业务中。然而,区块链技术的专业性使得其集成开发成为一项挑战。针对此,广东中创智慧科技有限公司基于国产开源联盟链 FISCO BCOS 推出了区块链集成开发平台。该平台基于区块链技术,提供一套全面的区块链开发工具和开发环境,支持开发者快速开发和部署区块链应用。此外,该平台还可以提供一套全面的区块链开发教程和文档,帮助开发者快速上手区块链开发。