`
hx.19890101
  • 浏览: 109206 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

标签栏与选取器的使用

    博客分类:
  • ios
阅读更多
我们要实现下面的效果,就是通过下方的标签栏切换视图来显示不同的选取器






首先创建工程,选择Window-Based Application模板,取项目名称为Picker。生成后选中Classes文件夹,从File中选择new File,再选择UIViewController subclass图标,顺便点选下面的第三项-with xib for user interface,分别取名为DatePicker,SingleComponentPicker,DoubleComponentPicker,生成之后将.xib文件拖入Resources文件夹中。
先添加根视图控制器,单击PickerAppDelegate.h类

#import <UIKit/UIKit.h>
@interface PickersAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
	UITabBarController *rootController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;
@end

单击PickerAppDelegate.m
@synthesize window;
@synthesize rootController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [window addSubview:rootController.view];
    [window makeKeyAndVisible];
    return YES;
}
- (void)dealloc {
    [rootController release];
    [window release];
    [super dealloc];
}

双击MainWindow.xib,从库中拖出一个Tab Bar Controller到nib主窗口,这样会出现一个新的窗口

起初下面只有两个标签,图上的3个标签是又添加上去了1个,选中新出现的窗口,按下花+1打开他的属性,点+,就能添加标签。

为了使每一个标签都能与对应的nib相关联,我们选中第一个。保留title为空,将nib name指定为DatePickerView,按花+4,将类改为DatePicker,再点中标签可以改变标签的名称。以此类推,完成剩下标签的相关连。



再nib主窗口中按住Ctrl将Picker App Delegate拖到Tab Bar Controller图标中。
单击DatePicker.h,完成代码

#import <UIKit/UIKit.h>
@interface DatePicker : UIViewController {
	IBOutlet UIDatePicker *datePicker;
}
@property (nonatomic,retain) UIDatePicker *datePicker;
-(IBAction)buttonPressed;
@end

单击DatePicker.m,完成代码
@synthesize datePicker;
-(IBAction)buttonPressed{
	NSData *selected = [datePicker date];
	NSString *message = [[NSString alloc] initWithFormat:@"The date and time is:%@",selected];
	UIAlertView *alert = [[UIAlertView alloc]
						  initWithTitle:@"Data and Time Selected" 
						  message:message 
						  delegate:nil 
						  cancelButtonTitle:@"Yes,I did" 
						  otherButtonTitles:nil];
	[alert show];
	[alert release];
	[message release];
}
- (void)viewDidLoad {
	NSDate *now = [[NSDate alloc] init];
	[datePicker setDate:now animated:YES];
	[now release];
    [super viewDidLoad];
}
- (void)dealloc {
	[datePicker release];
    [super dealloc];
}

这样第一个视图就完成了。
进行第二个,单个组件选取器,在SingleComponentPicker.h中声明输出口和操作

#import <UIKit/UIKit.h>
@interface SinglecomponentPickerViewController : UIViewController 
	<UIPickerViewDelegate,UIPickerViewDataSource>{
		IBOutlet UIPickerView *singlePicker;
		NSArray *pickerData;
}
@property (nonatomic,retain) UIPickerView *singlePicker;
@property (nonatomic,retain) NSArray *pickerData;
-(IBAction)buttonPressed;
@end

在相应的.xib中创建相应视图,一个Picker View和一个按钮,并完成如下关联




在相应.m文件中进行编码
@synthesize singlePicker;
@synthesize pickerData;
-(IBAction)buttonPressed{
	NSInteger row = [singlePicker selectedRowInComponent:0];
	NSString *selected = [pickerData objectAtIndex:row];
	NSString *title = [[NSString alloc] initWithFormat:@"You selected %@!",selected];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
						message:@"Thank you for choosing!" 
						delegate:nil 
						cancelButtonTitle:@"you'are welcome." 
						otherButtonTitles:nil];
	[alert show];
	[alert release];
	[title release];
}
- (void)viewDidLoad {
	NSArray *array = [[NSArray alloc] initWithObjects:@"Inter Milan",@"AC Milan",@"Arsenal",@"Liverpool",@"Chelsea",@"Newcastle",@"Manchester United",@"Real Madrid",nil];
	self.pickerData = array;
	[array release];
}
- (void)dealloc {
	[singlePicker release];
	[pickerData release];
    [super dealloc];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
	return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
	return [pickerData objectAtIndex:row];
}
@end

第二个视图也就完成了,下面创建第三个,在DoubleComponentPicker.h中声明输出口和操作
#import <UIKit/UIKit.h>
#define kFillingComponent 0
#define kBreadComponent 1
@interface DoublecomponentPickerViewController : UIViewController 
			<UIPickerViewDelegate,UIPickerViewDataSource>{
				IBOutlet UIPickerView *doublePicker;
				NSArray *fillingTypes;
				NSArray *breadTypes;
}
@property (nonatomic,retain)UIPickerView *doublePicker;
@property (nonatomic,retain)NSArray *fillingTypes;
@property (nonatomic,retain)NSArray *breadTypes;
-(IBAction)buttonPressed;
@end

控件的形式和关联和第二个视图中一样,可以参考。在相应的.m文件中进行编码
@synthesize doublePicker;
@synthesize fillingTypes;
@synthesize breadTypes;
-(IBAction)buttonPressed{
	NSInteger breadRow = [doublePicker selectedRowInComponent:kBreadComponent];
	NSInteger fillingRow = [doublePicker selectedRowInComponent:kFillingComponent];
	NSString *bread = [breadTypes objectAtIndex:breadRow];
	NSString *filling = [fillingTypes objectAtIndex:fillingRow];
	NSString *message = [[NSString alloc] initWithFormat:@"your %@ on %@ bread will be right up.",filling,bread];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you" 
						message:message 
						delegate:nil 
						cancelButtonTitle:@"Great" 
						otherButtonTitles:nil];
	[alert show];
	[alert release];
	[message release];
}
- (void)viewDidLoad {
    NSArray *breadArray = [[NSArray alloc] initWithObjects:@"Inter Milan",@"AC Milan",@"Arsenal",@"Liverpool",@"Chelsea",@"Newcastle",@"Manchester United",@"Real Madrid",nil];
	self.breadTypes = breadArray;
	[breadArray release];
	
	NSArray *fillingArray = [[NSArray alloc] initWithObjects:@"Raul",@"AC Milan",@"Arsenal",@"Liverpool",@"Chelsea",@"Newcastle",@"Manchester United",@"Real Madrid",nil];
	self.fillingTypes = fillingArray;
	[fillingArray release];
}
- (void)dealloc {
	[doublePicker release];
	[breadTypes release];
	[fillingTypes release];
    [super dealloc];
}
#pragma mark -
#pragma mark Picker Data Source Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	
	return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
	if (component == kBreadComponent) {
		return [self.breadTypes count];
	}
	return [self.fillingTypes count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
	if (component == kBreadComponent) {
		return [self.breadTypes objectAtIndex:row];
	}
	return [self.fillingTypes objectAtIndex:row];
}
@end

这样就结束了
  • 大小: 110.6 KB
  • 大小: 100.3 KB
  • 大小: 111.8 KB
  • 大小: 22.2 KB
  • 大小: 24 KB
  • 大小: 20.3 KB
  • 大小: 17.3 KB
  • 大小: 18.7 KB
  • 大小: 27.4 KB
分享到:
评论

相关推荐

    《Iphone开发基础教程》第七章 标签栏与选取器

    《Iphone开发基础教程》第七章 标签栏与选取器,源代码下载!具体的讲解在我的blog上面,blog.sina.com.cn/wanmeiguanjun.欢迎指点!

    WPF自定义控件例子,包括标签栏颜色选取

    WPF自定义控件例子,包括标签栏颜色选取,可自动跳转定义的图片和Textblock块内容

    iphone3超详细开发教程

    7标签栏与选取器 8表示图简介 9导航控制器和师表图 10应用吃呢光绪设置和用户默认设置 11基本数据持久性 12使用Quartz和OpenGL绘图 13轻击,触摸和受势 14使用 CoreLocation定位功能 15加速计 16iphone照相机和照片库...

    《iPhone开发基础教程》(PDF电子版)光碟配套(源代码)

    第七章 标签栏与选取器 第八章 表示图简介 第九章 导航控制器和表示图 第十章 应用程序设置和用户默认设置 第十一章 基本数据持久性 第十二章 使用Quartz和OpenGL绘制 第十三章 轻击、触摸和手势 第十四章 我在哪里...

    iOS 5基础教程

    第7章 标签栏与选取器 129 第8章 表视图简介 172 第9章 导航控制器和表视图 222 第10章 storyboard 284 第11章 iPad开发注意事项 307 第12章 应用程序设置和用户默认设置 330 第13章 保存数据 358 第14章 ...

    iPhone开发基础教程PDF(含源代码)

     第7章 标签栏与选取器  第8章 表视图简介  第9章 导航控制器和表视图  第10章 应用程序设置和用户默认设置  第11章 基本数据持久性  第12章 使用Quartz和OpenGL绘图  第13章 轻击、触摸和手势  第14章 我在...

    iphone3开发基础教程

    第7章 标签栏与选取器 104 7.1 Pickers应用程序 104 7.2 委托和数据源 106 7.3 建立工具栏框架 106 7.3.1 创建文件 107 7.3.2 添加根视图控制器 108 7.4 实现日期选取器 112 7.5 实现单个组件选取器 114 7.5.1 声明...

    微软UWP开发教程_2263页.pdf

    日历日期选取器 日历视图 日期选取器 时间选取器 滚动和布局 Expander 滚动和平移控件 语义式缩放 双窗格视图 状态和信息 进度 工具提示 信息栏 文本 概述 自动建议框 文本块 RTF 块 文本框 富编辑框 密码框 数字框 ...

    前端css+html+布局笔记

    这种样式只会对当前标签起作用,不能对样式进行复用,不方便后期维护,不推荐使用 2.内部样式表 将样式表编写到head中的style标签中 &lt;style type="text/css"&gt;&lt;/style&gt; 使用内部样式表,进一步将表现和结构...

    PowerToys ver0.57.2-x64Win10工具扩展

    颜色选择器,从屏幕中选取并编辑颜色 全窗口管理器,包括用于笔记本电脑扩展坞和取消扩展坞的特定布局 键盘快捷键管理器 Win+R 的替代快捷键 更好的 Alt+Tab 操作,包括浏览器的 Tab 标签集成和搜索当前运行的 App ...

    常用数据库控件的安装与使用方法

    ·第二步,在“Install Component”对话框的“Unit file name”栏中,使用“Browse”按钮将控件文件加进来;·第三步,单击“OK”按钮;·第四步,在弹出的“Confirm”对话框中,单击“Yes”按钮,安装该控件文件;...

    全能文件管理器Total Commander.zip

    内置文件夹标签功能:使用标签能够在多个文件夹间快速切换,左右窗口各个标签设置独立。 增强的文件搜索功能:可跨驱动器快速搜索文件及内容,支持多条件、通配符及正则表达式。 内置同步及比较功能:可多条件...

    Excel在财务管理中的应用.iso (随书光盘)

    1.3.6 工作表标签栏 14 1.3.7 状态栏 14 1.4 工作簿、工作表和单元格 15 1.4.1 工作簿 15 1.4.2 工作表 21 1.4.3 单元格 22 1.5 退出Excel 2007 27 第2章 创建标准的财务表格 2.1 数据输入 28 2.1.1 手工录入单元格...

    音乐照相地图

    这个小应用结合了导航控制器和标签栏,有xib,也有手写界面,结合了本地音乐播放器、本地视频播放器,在照片库了选取照片,还有定位经纬度,很适合初学者,注释有一点点,在这里共享给初学者,希望对大家有帮助

    电子表格处理软件应用计算机应用基础全套ppt文档.pptx

    复制、粘贴(zhāntiē)与移动单元格 数据(shùjù)筛选 添加(tiān jiā)数据透视表的数据字段 Excel基本操作 工作表中的数据也可以使用记录单输入(shūrù),下面演示使用记录单追加数据操作。 第十页,共67页。 ...

    超实用的jQuery代码段

    《超实用的jQuery代码段》从jQuery框架的使用原理与应用场景出发,对最实用的jQuery代码段进行了全方位的介绍和演示。全书分为11章,包含网页效果、DOM元素与属性、HTML事件、CSS样式、用户输入自动完成、拖放、图形...

Global site tag (gtag.js) - Google Analytics