iOSTab bar
http://www.apkbus.com/android-130504-1-1.html
#import
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface ViewController : UIViewController
{
UIDatePicker *datePicker;
UITabBar *tabBar;
FirstViewController *firstViewController;
SecondViewController *secondViewCOntroller;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 初始化Tab Bar
tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
//设置Tab Bar上的交互属性为YES
[tabBar setUserInteractionEnabled:YES];
// 设置代理
[tabBar setDelegate:self];
// 设置TabBar的背景颜色
[tabBar setBackgroundColor:[UIColor purpleColor]];
// 设置tabBar的透明度
tabBar.alpha = 0.5f;
// 定义一个可变数组存放tab Bar Item
NSMutableArray *tabBarArray= [NSMutableArray array];
// 初始化一个Tab Bar Item 到数组中
[tabBarArray addObject:[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites
tag:1]];
[tabBarArray addObject:[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory
tag:2]];
UIImage *image = [UIImage imageNamed:@"test48.png"];
[tabBarArray addObject:[[UITabBarItem alloc]initWithTitle:@"测试" image:image tag:3]];
// 给Tab Bar 添加数组里的tab Bar Item
[tabBar setItems:tabBarArray animated:NO];
[self.view addSubview:tabBar];
// datePicker宽度320像素和高度216像素,系统都设置好了,只需设置一下他的远点坐标,
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
// 设置datePicker显示模式
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
//DatePicker属于UIControl子类,可以触发事件,当滚动滑轮滑轮停下后就调用这个方法了
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
}
-(void)dateChanged:(id)sender
{
UIDatePicker *control = (UIDatePicker*)sender;
// 把当前控件设置的时间赋给date
NSDate *date = control.date;
// 将NSDate格式装换成NSString类型
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
// 设置日历显示格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// 把日历时间传给字符串
NSString *strDate = [dateFormatter stringFromDate:date];
NSString *message = [[NSString alloc]initWithFormat:@"你选取的时间是:%@",strDate];
// 弹出一个警告
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
// 显示警告
[alert show];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
-(void)tabBar:(UITabBar *)tabBarBar didSelectItem:(UITabBarItem *)item
{
if (item.tag == 1) {
// [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
NSLog(@"---->%d",item.tag);
if(firstViewController.view.superview==nil)
{
if (firstViewController.view==nil) {
FirstViewController *firstView = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
firstViewController=firstView;
}
[secondViewCOntroller.view removeFromSuperview];
// [self.view insertSubview:firstViewController.view atIndex:0];
[self.view addSubview:firstViewController.view];
}
else {
[secondViewCOntroller.view removeFromSuperview];
// [self.view insertSubview:firstViewController.view atIndex:0];
[self.view addSubview:firstViewController.view];
}
}
if (item.tag == 2) {
if (secondViewCOntroller.view.superview==nil) {
if (secondViewCOntroller.view == nil)
{
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewCOntroller=secondView;
}
[firstViewController.view removeFromSuperview];
[self.view insertSubview:secondViewCOntroller.view atIndex:0];
}
else {
[firstViewController.view removeFromSuperview];
[self.view insertSubview:secondViewCOntroller.view atIndex:0];
}
}
NSLog(@"---->%d",item.tag);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。