Iphone [Tab Bar实现多view切换,Picker,DataPicter实现
用Tab Bar Controller处理IPhone多个view切换,
而且还附有创建空项目,picker和DataPicker的实现!
具体步骤:
1.创建一个空项目,选择User Interface->View,命名为rootView。
2.然后在控件面板中拖一个Tab Bar Controller的控件,可以往里面添加Tab Button,将Tab Bar Controller的File's Owner改成AppDelegate,可以将其每一个Item View的Class属性改成对于的ViewController
3.在Appdelegate.m中设置根view
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- //通过目录导入xib文件中所有的内容
- NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"rootView" owner:self options:nil];
- NSLog(@"%@",arr);
- self.window.rootViewController = self.rootView;
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
- }
4.DateViewPicker的实现
dateViewController.h:
- #import <UIKit/UIKit.h>
- @interface dataViewController : UIViewController
- @property (retain, nonatomic) IBOutlet UIDatePicker *datePicker;
- - (IBAction)click:(id)sender;
- @property (retain, nonatomic) IBOutlet UILabel *lblShow;
- @end
dateViewController.m:
- #import "dataViewController.h"
- @interface dataViewController ()
- @end
- @implementation dataViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //self.datePicker.date = [NSData date];
- [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeDate) userInfo:nil repeats:YES];
- }
- -(void)changeDate
- {
- self.datePicker.date = [NSDate date];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)dealloc {
- [_datePicker release];
- [_lblShow release];
- [super dealloc];
- }
- - (IBAction)click:(id)sender {
- NSData *date = self.datePicker.date;
- NSDateFormatter *formater=[[[NSDateFormatter alloc] init] autorelease];
- //设置日期格式
- formater.dateFormat = @"yyyy-MM-dd HH:mm:ss";//HH代表24时制,hh代表12时制
- NSString *str = [formater stringFromDate:date];
- //设置时区
- formater.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease];
- date = [formater dateFromString:@"2013-08-15 14:03:00"];
- NSLog(@"字符串转化成日期是:%@",date);
- NSLog(@"%@",str);
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前时间" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
- [alert show];
- self.lblShow.text = str;
- }
- @end
PickerView的实现:
viewController.h:
#import <UIKit/UIKit.h>
//注意:在使用pickerView的时候要将控件拖向File's Owner,将DataSourse和Delegate指向File's Owner,并且要实现他的两个协议
@interface SecondViewController :UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property (retain,nonatomic)IBOutletUIPickerView *picker;
- (IBAction)click:(id)sender;
@property(nonatomic,retain)NSArray *array;
@end
viewController.m:
- #import "SecondViewController.h"
- @interface SecondViewController ()
- @end
- @implementation SecondViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSArray *arr = [NSArray arrayWithObjects:@"shanghai",@"nanjign",@"tianjin",@"nantong",@"beijing",nil];
- self.array = arr;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)dealloc {
- [_picker release];
- [super dealloc];
- }
- #pragma mark datasourse
- //每个组件有多少行数据
- -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- return [self.array count];
- }
- //PickerView有几个组件
- -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- return 1;
- }
- #pragma mark delegate
- //每个组件中的每行显示什么数据
- -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
- {
- return [self.array objectAtIndex:row];
- }
- - (IBAction)click:(id)sender {
- int row = [self.picker selectedRowInComponent:0];//获得第几个组件中被选中的行数
- NSString *str = [self.array objectAtIndex:row];//通过第几行,在数据中获得被选中的字符串
- UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"title" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
- [alert show];
- }
- @end
效果图:
如果有两个Component的话:
viewController.h:
- #import <UIKit/UIKit.h>
- @interface FirstViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
- @property (retain, nonatomic) IBOutlet UIPickerView *picker;
- @property(nonatomic,retain)NSArray *array;
- @property(nonatomic,retain)NSArray *array2;
- - (IBAction)click:(id)sender;
- @end
viewController.m:
- #import "FirstViewController.h"
- #define component_0 0
- #define other_component 1
- @interface FirstViewController ()
- @end
- @implementation FirstViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSArray *array = [NSArray arrayWithObjects:@"上海",@"北京",@"天津",@"成都",@"台湾",@"香港",@"江苏",@"武汉",@"黑龙江",@"浙江", nil];
- self.array = array;
- array = @[@"路飞",@"乔巴",@"香吉士",@"索隆",@"娜美",@"罗宾",@"乌索普",@"弗兰奇",@"骷髅头"];
- self.array2 = array;
- //用来设置默认选项
- [self.picker selectRow:2 inComponent:component_0 animated:YES];
- [self.picker selectRow:3 inComponent:other_component animated:YES];
- }
- //每个组件有几行数据
- -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- //创建数据
- if(component == component_0)
- {
- return [self.array count]; //动态获取数字
- }else{
- return [self.array2 count];
- }
- }
- #pragma mark delegate
- //每个组件的每行显示什么数据
- -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
- {
- if(component == component_0)
- {
- return [self.array objectAtIndex:row];
- }else{
- return [self.array2 objectAtIndex:row];
- }
- }
- -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- return 2;
- }
- //当你对一个pickerView进行了一次操作之后都会被调用
- -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
- {
- NSString *str = nil;
- if(component == component_0)
- {
- str = [self.array objectAtIndex:row];
- }
- else
- {
- str = [self.array2 objectAtIndex:row];
- }
- NSLog(@"%@",str);
- }
- - (void)dealloc {
- [_picker release];
- [_array release];
- [_array2 release];
- [super dealloc];
- }
- - (IBAction)click:(id)sender {
- int row = [self.picker selectedRowInComponent:component_0]; //第几个组件中被选中的行数
- NSString * str = [self.array objectAtIndex:row]; //通过第几行,在数据中获得被选中的字符串
- row = [self.picker selectedRowInComponent:other_component];
- NSString *str1 = [self.array2 objectAtIndex:row];
- NSString *str3 = [NSString stringWithFormat:@"%@ %@",str,str1];
- UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Title" message:str3 delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [alert show];
- }
- @end
Iphone [Tab Bar实现多view切换,Picker,DataPicter实现的更多相关文章
- iOS开发:使用Tab Bar切换视图
iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...
- Tab Bar Controller和Navigation Controller混合使用详细教程
在IPHONE上,NAV和TAB混合使用的案例很多.但很多书籍都没详细介绍这个是怎么使用的.我也找了很久才弄清楚怎么做.现在分享给大家. 1.先建立一个Window-based Application ...
- iOS第八课——Navigation Controller和Tab bar Controller
今天我们要学习Navigation Controller和Tab bar Controller. Navigation Controller是iOS编程中比较常用的一种容器,用来管理多个视图控制器. ...
- 自定义tab bar控件 学习资料
http://blog.csdn.net/zoeice/article/details/8068671 import java.util.Vector; import android.content. ...
- IOS学习之基于IOS7的tab bar
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28129473 作者:小马 什么是tabbar? 先几张图: 上图中蓝色框 ...
- 学习笔记:Tab Bar 控件使用详解
注意这里是:Tab Bar 不是Tab Bar Controller. Tab bar是继承UIView,所以可以添加到ViewController里.是View就可以add到另一个View上去.Ta ...
- 自定义custom Tab Bar
iOS提供的Tab Bar比较简单,我们常常有些别样的需求,此时往往需要自行自定义Tab Bar,如下图所示: 如图所示,需要在某个页面中添加一个类 ...
- tab bar controller
下面记一下怎样通过代码的方式为选项卡添加视图. 1.创建一个基于Empty Application的项目 2.创建两个新类,基类选择UIViewController,勾选With XIB for us ...
- 纯CSS完成tab实现5种不同切换对应内容效果
很常用的一款特效纯CSS完成tab实现5种不同切换对应内容效果 实例预览 下载地址 实例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
随机推荐
- 【云计算】Cloudify-基于TOSCA规范的开源云应用编排系统
.cloudify-manager-blueprints:https://github.com/cloudify-cosmo/cloudify-manager-blueprints/tree/3. ...
- Javascript——Context和Scope的一些学习总结
1.1.1 摘要 在我们学习Javascript过程中,常常会遇到作用域(Scope)和执行上下文(Context)等概念.其中,执行上下文与this关键字的关系密切. 有面向对象编程经验的各位,对于 ...
- Java 中常用缓存Cache机制的实现《二》
所谓缓存,就是将程序或系统经常要调用的对象存在内存中,一遍其使用时可以快速调用,不必再去创建新的重复的实例.这样做可以减少系统开销,提高系统效率. AD: Cache 所谓缓存,就是将程序或系统经常要 ...
- NGUI 图片变灰
效果图 1.先准备好一个变灰shader.代码如下 Shader "Custom/Gray" { Properties { _MainTex ("Base (RGB), ...
- pgpool介绍和安装经验
Pgpool的介绍 一.介绍 是一个工作在PostgreSQL多服务器和PostgreSQL数据库客户端之间的中间件. 二.概念图 三.功能 连接池:pgpool -Ⅱ保存 连 接到PostgreSQ ...
- Greedy:Physics Experiment(弹性碰撞模型)(POJ 3848)
物理实验 题目大意:有一个与地面垂直的管子,管口与地面相距H,管子里面有很多弹性球,从t=0时,第一个球从管口求开始下落,然后每1s就会又有球从球当前位置开始下落,球碰到地面原速返回,球与球之间相碰会 ...
- poj 2389.Bull Math 解题报告
题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次 ...
- tableView性能优化
针对滑动时出现卡的现象 参考:http://blog.sina.cn/dpool/blog/s/blog_b638dc890101ep3x.html?plg_nld=1&plg_auth=1& ...
- 【python升级录】--列表,元组
本节内容 字符串格式化输出 数据运算 for循环 while循环 列表 元组 [字符串格式化输出] 占位符 %s—string,%d—digital,%f —float # __author:&quo ...
- iOS应用架构谈(三):View层的组织和调用方案(下)
iOS客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答iOS应用架构中的种种问题,本文是其中的第二篇,主要讲View层的组织和调用方案.下篇主要讨论做View层架构的 ...