用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

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4. //通过目录导入xib文件中所有的内容
  5. NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"rootView" owner:self options:nil];
  6. NSLog(@"%@",arr);
  7. self.window.rootViewController = self.rootView;
  8. self.window.backgroundColor = [UIColor whiteColor];
  9. [self.window makeKeyAndVisible];
  10. return YES;
  11. }

4.DateViewPicker的实现

dateViewController.h:

  1. #import <UIKit/UIKit.h>
  2. @interface dataViewController : UIViewController
  3. @property (retain, nonatomic) IBOutlet UIDatePicker *datePicker;
  4. - (IBAction)click:(id)sender;
  5. @property (retain, nonatomic) IBOutlet UILabel *lblShow;
  6. @end

dateViewController.m:

  1. #import "dataViewController.h"
  2. @interface dataViewController ()
  3. @end
  4. @implementation dataViewController
  5. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  6. {
  7. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  8. if (self) {
  9. // Custom initialization
  10. }
  11. return self;
  12. }
  13. - (void)viewDidLoad
  14. {
  15. [super viewDidLoad];
  16. //self.datePicker.date = [NSData date];
  17. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeDate) userInfo:nil repeats:YES];
  18. }
  19. -(void)changeDate
  20. {
  21. self.datePicker.date = [NSDate date];
  22. }
  23. - (void)didReceiveMemoryWarning
  24. {
  25. [super didReceiveMemoryWarning];
  26. // Dispose of any resources that can be recreated.
  27. }
  28. - (void)dealloc {
  29. [_datePicker release];
  30. [_lblShow release];
  31. [super dealloc];
  32. }
  33. - (IBAction)click:(id)sender {
  34. NSData *date = self.datePicker.date;
  35. NSDateFormatter *formater=[[[NSDateFormatter alloc] init] autorelease];
  36. //设置日期格式
  37. formater.dateFormat = @"yyyy-MM-dd HH:mm:ss";//HH代表24时制,hh代表12时制
  38. NSString *str  = [formater stringFromDate:date];
  39. //设置时区
  40. formater.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease];
  41. date = [formater dateFromString:@"2013-08-15 14:03:00"];
  42. NSLog(@"字符串转化成日期是:%@",date);
  43. NSLog(@"%@",str);
  44. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前时间" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
  45. [alert show];
  46. self.lblShow.text = str;
  47. }
  48. @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:

  1. #import "SecondViewController.h"
  2. @interface SecondViewController ()
  3. @end
  4. @implementation SecondViewController
  5. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  6. {
  7. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  8. if (self) {
  9. // Custom initialization
  10. }
  11. return self;
  12. }
  13. - (void)viewDidLoad
  14. {
  15. [super viewDidLoad];
  16. NSArray *arr = [NSArray arrayWithObjects:@"shanghai",@"nanjign",@"tianjin",@"nantong",@"beijing",nil];
  17. self.array = arr;
  18. }
  19. - (void)didReceiveMemoryWarning
  20. {
  21. [super didReceiveMemoryWarning];
  22. // Dispose of any resources that can be recreated.
  23. }
  24. - (void)dealloc {
  25. [_picker release];
  26. [super dealloc];
  27. }
  28. #pragma mark datasourse
  29. //每个组件有多少行数据
  30. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  31. {
  32. return [self.array count];
  33. }
  34. //PickerView有几个组件
  35. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  36. {
  37. return 1;
  38. }
  39. #pragma mark delegate
  40. //每个组件中的每行显示什么数据
  41. -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  42. {
  43. return [self.array objectAtIndex:row];
  44. }
  45. - (IBAction)click:(id)sender {
  46. int row = [self.picker selectedRowInComponent:0];//获得第几个组件中被选中的行数
  47. NSString *str = [self.array objectAtIndex:row];//通过第几行,在数据中获得被选中的字符串
  48. UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"title" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
  49. [alert show];
  50. }
  51. @end

效果图:

如果有两个Component的话:

viewController.h:

  1. #import <UIKit/UIKit.h>
  2. @interface FirstViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
  3. @property (retain, nonatomic) IBOutlet UIPickerView *picker;
  4. @property(nonatomic,retain)NSArray *array;
  5. @property(nonatomic,retain)NSArray *array2;
  6. - (IBAction)click:(id)sender;
  7. @end

viewController.m:

  1. #import "FirstViewController.h"
  2. #define component_0 0
  3. #define other_component 1
  4. @interface FirstViewController ()
  5. @end
  6. @implementation FirstViewController
  7. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  8. {
  9. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  10. if (self) {
  11. // Custom initialization
  12. }
  13. return self;
  14. }
  15. - (void)viewDidLoad
  16. {
  17. [super viewDidLoad];
  18. NSArray *array = [NSArray arrayWithObjects:@"上海",@"北京",@"天津",@"成都",@"台湾",@"香港",@"江苏",@"武汉",@"黑龙江",@"浙江", nil];
  19. self.array = array;
  20. array = @[@"路飞",@"乔巴",@"香吉士",@"索隆",@"娜美",@"罗宾",@"乌索普",@"弗兰奇",@"骷髅头"];
  21. self.array2 = array;
  22. //用来设置默认选项
  23. [self.picker selectRow:2 inComponent:component_0 animated:YES];
  24. [self.picker selectRow:3 inComponent:other_component animated:YES];
  25. }
  26. //每个组件有几行数据
  27. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  28. {
  29. //创建数据
  30. if(component == component_0)
  31. {
  32. return [self.array count];   //动态获取数字
  33. }else{
  34. return [self.array2 count];
  35. }
  36. }
  37. #pragma mark delegate
  38. //每个组件的每行显示什么数据
  39. -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  40. {
  41. if(component == component_0)
  42. {
  43. return [self.array objectAtIndex:row];
  44. }else{
  45. return [self.array2 objectAtIndex:row];
  46. }
  47. }
  48. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  49. {
  50. return 2;
  51. }
  52. //当你对一个pickerView进行了一次操作之后都会被调用
  53. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  54. {
  55. NSString *str = nil;
  56. if(component == component_0)
  57. {
  58. str = [self.array objectAtIndex:row];
  59. }
  60. else
  61. {
  62. str = [self.array2 objectAtIndex:row];
  63. }
  64. NSLog(@"%@",str);
  65. }
  66. - (void)dealloc {
  67. [_picker release];
  68. [_array release];
  69. [_array2 release];
  70. [super dealloc];
  71. }
  72. - (IBAction)click:(id)sender {
  73. int row = [self.picker selectedRowInComponent:component_0];   //第几个组件中被选中的行数
  74. NSString * str = [self.array objectAtIndex:row];   //通过第几行,在数据中获得被选中的字符串
  75. row = [self.picker selectedRowInComponent:other_component];
  76. NSString *str1 = [self.array2 objectAtIndex:row];
  77. NSString *str3 = [NSString stringWithFormat:@"%@ %@",str,str1];
  78. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Title" message:str3 delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  79. [alert show];
  80. }
  81. @end

Iphone [Tab Bar实现多view切换,Picker,DataPicter实现的更多相关文章

  1. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  2. Tab Bar Controller和Navigation Controller混合使用详细教程

    在IPHONE上,NAV和TAB混合使用的案例很多.但很多书籍都没详细介绍这个是怎么使用的.我也找了很久才弄清楚怎么做.现在分享给大家. 1.先建立一个Window-based Application ...

  3. iOS第八课——Navigation Controller和Tab bar Controller

    今天我们要学习Navigation Controller和Tab bar Controller. Navigation Controller是iOS编程中比较常用的一种容器,用来管理多个视图控制器. ...

  4. 自定义tab bar控件 学习资料

    http://blog.csdn.net/zoeice/article/details/8068671 import java.util.Vector; import android.content. ...

  5. IOS学习之基于IOS7的tab bar

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28129473 作者:小马 什么是tabbar? 先几张图:      上图中蓝色框 ...

  6. 学习笔记:Tab Bar 控件使用详解

    注意这里是:Tab Bar 不是Tab Bar Controller. Tab bar是继承UIView,所以可以添加到ViewController里.是View就可以add到另一个View上去.Ta ...

  7. 自定义custom Tab Bar

    iOS提供的Tab Bar比较简单,我们常常有些别样的需求,此时往往需要自行自定义Tab Bar,如下图所示:                           如图所示,需要在某个页面中添加一个类 ...

  8. tab bar controller

    下面记一下怎样通过代码的方式为选项卡添加视图. 1.创建一个基于Empty Application的项目 2.创建两个新类,基类选择UIViewController,勾选With XIB for us ...

  9. 纯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 ...

随机推荐

  1. 2小时入门Robot Framework

    1.介绍 1.1.介绍Robot Robot Framework是一个基于关键字驱动的自动化测试框架.通过该框架,测试人员可使用python封装关键字,并在非代码环境下使用关键字构建可被执行的测试用例 ...

  2. mysql性能优化-简易版

    mysql性能优化 sql语句优化 如何发现有问题的sql? 开启mysql慢查询 show variables like 'slow_query_log' set global slow_query ...

  3. ios 单一线程中的Runloop机制会导致线程安全问题吗?

    今天在处理多线程突然想到一个问题,多核处理器会不会导致,单一线程中,由runloop分发的2个函数同时执行呢?进而同时修改同一个变量,产生bug? 我做了以下的测试: - (void)viewDidL ...

  4. Java for LeetCode 234 Palindrome Linked List

    解题思路: O(1)的空间复杂度,意味着不能通过开一个List来解决问题.我们可以把List分成前后两个部分,后半部分通过指针的相互赋值进行翻转即可. JAVA实现如下: public static ...

  5. K3数据库表结构

    K3数据库表结构查看方法,直接在数据库中打开表 t_TableDescription,其中即各表及其与K3功能的对应关系 也可直接查询: select * from t_TableDescriptio ...

  6. Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)

    前言:经常会看到有一些app的banner界面可以实现循环播放多个广告图片和手动滑动循环.本以为单纯的ViewPager就可以实现这些功能.但是蛋疼的事情来了,ViewPager并不支持循环翻页.所以 ...

  7. bootstrap弹出提示窗口功能

    大家常用弹出提示成功或失败时候喜欢用alert,如果不用这个,写个弹窗又麻烦,bootstrap中就有个方法 alert("操作成功"); commonAlert("操作 ...

  8. .net在当前日期的基础上加一天

    比如今天是:2015-11-10 18:57:01,在这个基础上加一天,那么就是2015-11-11 18:57:01,代码如下: DateTime now_dt = DateTime.Now; ). ...

  9. mongoDB 3.0以前版本 - 入门指南、示例

    一.准备工作 1. 下载mongoDB 下载地址:http://www.mongodb.org/downloads 选择合适你的版本 相关文档:http://www.mongodb.org/displ ...

  10. 模拟赛1031d1

    NP(np)Time Limit:1000ms Memory Limit:64MB题目描述LYK 喜欢研究一些比较困难的问题,比如 np 问题.这次它又遇到一个棘手的 np 问题.问题是这个样子的:有 ...