009 在Xcode4.5上创建IOS6.0应用 (高级控件 拾取器)

2023-12-10 02:38

本文主要是介绍009 在Xcode4.5上创建IOS6.0应用 (高级控件 拾取器),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

IOS中的高级控件拾取器

在学习这个高级控件之前先建立好项目




然后按照在   008 在Xcode4.5上创建IOS6.0应用 (多视图应用程序)》中的方法建立两个视图

最后在AppDelegate.m文件中添加如下代码(图中的红框内代码为新添加的代码)
注意要在AppDelegate.m文件中引入




第一种:时间拾取器
时间拾取器这个在实现起来最为简单
第一步在视图中添加控件


第二步:设置它的属性


第三步:编码实现其中的效果
FirstViewController.h
@interface FirstViewController : UIViewController{IBOutlet UIDatePicker *uidataPicker;
}@property(nonatomic,retain)UIDatePicker *uidataPicker;-(IBAction)onClick:(id)sender;@end


FirstViewController.m
@synthesize uidataPicker;-(IBAction)onClick:(id)sender{NSDate *selected = [uidataPicker date];NSString *message = [[NSString alloc] initWithFormat:@"您选择的日期是: %@",selected];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择的日期"message:message delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];[alert show];[alert release];[message release];
}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {self.title = NSLocalizedString(@"First", @"First");self.tabBarItem.image = [UIImage imageNamed:@"first"];}return self;
}- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[uidataPicker release];[super dealloc];
}@end

第四步:为其控件连线


这样下来,一个简单的时间拾取器这样就完成了

第二种:单个标签拾取器(比较复杂)
第一步:加入控件-->实现输入输出--> 最后注意要输出数据一定要注意图中第3步


第二步:编码
需要注意的是要在SecondViewController.h文件中实现两个协议

< UIPickerViewDataSource,UIPickerViewDelegate>


源码如下:

@interface SecondViewController : UIViewController< UIPickerViewDataSource,UIPickerViewDelegate>{IBOutlet UIPickerView *pickView;NSArray * arr;
}@property (nonatomic,retain)UIPickerView *pickView;
@property (nonatomic,retain)NSArray * arr;-(IBAction)onClick:(id)sender;
@end

然后在SecondViewController.m文件中编码

实现协议后必须实现协议里面的三个方法分别为

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{}


然后必须在初始化的时候创建一个定义了拾取器中内容的数组

也就拾在ViewDidLoad方法中

NSArray *array = [[NSArrayalloc]initWithObjects:@"欧洲",@"南美洲", @"非洲",@"北美洲",@"亚洲",@"大洋洲",nil];


源代码如下:

@implementation SecondViewController@synthesize pickView;
@synthesize arr;-(IBAction)onClick:(id)sender{NSInteger row = [pickView selectedRowInComponent:0];NSString *selected = [arr objectAtIndex:row];NSString *title = [[NSString alloc] initWithFormat:@"您选择了 %@!",selected];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择的日期"message:titledelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];[alert show];[alert release];[title release];[selected release];
}//pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{return [arr objectAtIndex:row];
}//pragma mark 实现协议UIPickerViewDataSource方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 1;
}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{return [arr count];
}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {self.title = NSLocalizedString(@"Second", @"Second");self.tabBarItem.image = [UIImage imageNamed:@"second"];}return self;
}- (void)viewDidLoad
{[super viewDidLoad];NSArray *array = [[NSArray alloc] initWithObjects:@"欧洲",@"南美洲",@"非洲",@"北美洲",@"亚洲",@"大洋洲",nil];self.arr = array;[array release];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[pickView release];[arr release];[super dealloc];
}@end

最后再次提醒一定要加入上图第三步中的连线


最后再看看效果图



第三种:双标签拾取器(如果上面那个搞清楚了这个就相对比较简单)

由于这个只是把拾取器从1个变成了2个所以在定义集合变量的时候要定义两个

ThreeViewController.h

@interface ThreeViewController : UIViewController< UIPickerViewDataSource,UIPickerViewDelegate>{IBOutlet UIPickerView *pickView;NSArray * arr1;NSArray * arr2;
}@property (nonatomic,retain)UIPickerView *pickView;
@property (nonatomic,retain)NSArray * arr1;
@property (nonatomic,retain)NSArray * arr2;-(IBAction)onClick:(id)sender;@end

ThreeViewController.m文件也只要做点点的修改就行了,说白了,就拾一个变两个的过程

@implementation ThreeViewController@synthesize pickView;
@synthesize arr1;
@synthesize arr2;-(IBAction)onClick:(id)sender{NSInteger row1 = [pickView selectedRowInComponent:0];NSInteger row2 = [pickView selectedRowInComponent:1];NSString *selected1 = [arr1 objectAtIndex:row1];NSString *selected2 = [arr2 objectAtIndex:row2];NSString *title = [[NSString alloc] initWithFormat:@"您选择了%@中的%@!",selected1,selected2];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"选择"message:titledelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil, nil];[alert show];[alert release];[title release];[selected1 release];[selected2 release];
}//pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{if (component == 0) {return [arr1 objectAtIndex:row];}else if(component == 1){return [arr2 objectAtIndex:row];}else{return nil;}
}//pragma mark 实现协议UIPickerViewDataSource方法
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 2;
}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{if (component ==0) {return [arr1 count];}else if (component == 1){return [arr2 count];}else{return 0;}
}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {self.title = NSLocalizedString(@"Three", @"Three");self.tabBarItem.image = [UIImage imageNamed:@"first"];}return self;
}- (void)viewDidLoad
{[super viewDidLoad];NSArray *array1 = [[NSArray alloc] initWithObjects:@"欧洲",@"南美洲",@"非洲",@"北美洲",@"亚洲",@"大洋洲",nil];NSArray *array2 = [[NSArray alloc] initWithObjects:@"足球",@"篮球",@"羽毛球",@"乒乓球",@"保龄球",@"铅球",nil];self.arr1 = array1;self.arr2 = array2;[array1 release];[array2 release];
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[pickView release];[arr1 release];[arr2 release];[super dealloc];
}@end

第四种:双标签拾取器有联动效果(如果上面那个搞清楚了这个就相对比较简单)

有联动效果的拾取器相对与第三种稍微麻烦一点

首先来看看源代码

FourViewController.h

@interface FourViewController : UIViewController< UIPickerViewDataSource,UIPickerViewDelegate>{NSDictionary *date;IBOutlet UIPickerView *pickView;NSArray * arr1;NSArray * arr2;
}@property(nonatomic,retain)NSDictionary *date;@property (nonatomic,retain)UIPickerView *pickView;
@property (nonatomic,retain)NSArray * arr1;
@property (nonatomic,retain)NSArray * arr2;-(IBAction)onClick:(id)sender;
@end


FourViewController.m

@implementation FourViewController@synthesize date;
@synthesize pickView;
@synthesize arr1;
@synthesize arr2;//pragma mark 实现协议UIPickerViewDelegate方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{if (component == 0) {return [arr1 objectAtIndex:row];}else if(component == 1){return [arr2 objectAtIndex:row];}else{return nil;}
}//pragma mark 实现协议UIPickerViewDataSource方法
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 2;
}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{if (component ==0) {return [arr1 count];}else if (component == 1){return [arr2 count];}else{return 0;}
}//实现联动的效果
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{if(component ==0){NSString *selectedCol1 = [self.arr1 objectAtIndex:row];NSArray *array = [self.date objectForKey:selectedCol1];self.arr2 = array;[self.pickView reloadComponent:1];}
}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {self.title = NSLocalizedString(@"Four", @"Four");self.tabBarItem.image = [UIImage imageNamed:@"second"];}return self;
}- (void)viewDidLoad
{[super viewDidLoad];NSBundle * bundle = [NSBundle mainBundle];NSString * plistPath = [bundle pathForResource:@"statedictionary"ofType:@"plist"];NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];self.date = dict;[dict release];NSArray *col1 = [self.date allKeys];//下面这个方法是排序的意思NSArray *sorted = [col1 sortedArrayUsingSelector:@selector(compare:)];self.arr1 = sorted;NSArray *seleteCol1 = [self.arr1 objectAtIndex:0];NSArray *col2 = [self.date objectForKey:seleteCol1];self.arr2 = col2;}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}- (void)dealloc
{[date release];[pickView release];[arr1 release];[arr2 release];[super dealloc];
}
@end

代码中与上一个有小区别的是

多了一个联动是调用的方法

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component


还多了一个变量这个变量是字典变量

唯一不同的是这次获取内容是从文件中获取的


下面就来介绍一下定义文件的过程

第一步:看图建立文件



第二步:编辑文件中的内容(由于Xcode为我们提供了比较好的编辑工具所以文件编辑过程就不多做介绍)

但是要注意下图红框中的内容一但填错就得不到数据

在选择Root类型的时候一定要选择字典类型



最后看两张效果图片



最后拾取器就这样介绍完了,看了以上的介绍一定觉得拾取器还是比较简单的控键把。







这篇关于009 在Xcode4.5上创建IOS6.0应用 (高级控件 拾取器)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

mysql中的group by高级用法

《mysql中的groupby高级用法》MySQL中的GROUPBY是数据聚合分析的核心功能,主要用于将结果集按指定列分组,并结合聚合函数进行统计计算,下面给大家介绍mysql中的groupby用法... 目录一、基本语法与核心功能二、基础用法示例1. 单列分组统计2. 多列组合分组3. 与WHERE结合使

Java中的Lambda表达式及其应用小结

《Java中的Lambda表达式及其应用小结》Java中的Lambda表达式是一项极具创新性的特性,它使得Java代码更加简洁和高效,尤其是在集合操作和并行处理方面,:本文主要介绍Java中的La... 目录前言1. 什么是Lambda表达式?2. Lambda表达式的基本语法例子1:最简单的Lambda表

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自