看到很多项目中都采用的是Navigation加Tab Bar组合到一起,完成视图切换操作,在导航栏上添加基本按钮,给予响应事件,让应用给用户更好的体验,所以本菜鸟写了这个这样一个Demo,仅供学习
所创建工程模板是最后一个 Empty Application
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5489/724ed4c2-baaa-3816-96f4-52d8ec457918.png[/img]
[/img]
先看运行效果:
第一个视图,点击按钮切换视图,点击导航栏上按钮可以切换回去
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5491/f776d646-b182-3884-843a-fbdef1c1589e.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5493/73d3ad50-efa7-364c-9e6e-4306b37e97d9.png[/img]
[/img]
第二个视图设置了背景颜色和透明度 第三个视图添加了背景图片
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5495/490cabf2-15ad-3431-9661-ff0cf82f63fb.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5497/a7f5bfc9-833a-3844-aeb1-64ec93e62714.png[/img]
[/img]
第四个视图,在导航栏上添加了两个按钮,左边按钮属于自定义标题,右边按钮是系统的图标,点击左按钮弹出一个警告,右边按钮没有添加响应事件,点击后没反应
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5499/b0b22bcd-f9c5-33a1-8a23-60602318ebd4.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5501/2c32c67b-be48-313f-a676-e1ef36b8221f.png[/img]
[/img]
Tab Bar上添加的都是自定义图片
框架组合的主要代码,在AppDelegate.m中
第一个视图切换按钮响应事件
第四个视图添加两个按钮方法,在最后一个控制机的.m文件中的-(void)viewDidLoad方法中
所创建工程模板是最后一个 Empty Application
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5489/724ed4c2-baaa-3816-96f4-52d8ec457918.png[/img]
[/img]
先看运行效果:
第一个视图,点击按钮切换视图,点击导航栏上按钮可以切换回去
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5491/f776d646-b182-3884-843a-fbdef1c1589e.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5493/73d3ad50-efa7-364c-9e6e-4306b37e97d9.png[/img]
[/img]
第二个视图设置了背景颜色和透明度 第三个视图添加了背景图片
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5495/490cabf2-15ad-3431-9661-ff0cf82f63fb.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5497/a7f5bfc9-833a-3844-aeb1-64ec93e62714.png[/img]
[/img]
第四个视图,在导航栏上添加了两个按钮,左边按钮属于自定义标题,右边按钮是系统的图标,点击左按钮弹出一个警告,右边按钮没有添加响应事件,点击后没反应
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5499/b0b22bcd-f9c5-33a1-8a23-60602318ebd4.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0079/5501/2c32c67b-be48-313f-a676-e1ef36b8221f.png[/img]
[/img]
Tab Bar上添加的都是自定义图片
框架组合的主要代码,在AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
_tabBarController = [[UITabBarController alloc] init];
_tabBarController.delegate = self;
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
FourViewController *fourViewController = [[FourViewController alloc] initWithNibName:@"FourViewController" bundle:nil];
UINavigationController *navFirst = [[UINavigationController alloc] initWithRootViewController:firstViewController];
// 在加载图片是把标题都覆盖了,所以运行效果并没有显示这些文字
navFirst.title = @"第一个视图";
UINavigationController *navSecond = [[UINavigationController alloc] initWithRootViewController:secondViewController];
navSecond.title = @"第二个视图";
UINavigationController *navThird = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
navThird.title = @"第三个视图";
UINavigationController *navFour = [[UINavigationController alloc] initWithRootViewController:fourViewController];
navFour.title = @"第四个视图";
_tabBarController.viewControllers = [NSArray arrayWithObjects:navFirst,navSecond,navThird,navFour, nil];
_window.rootViewController = _tabBarController;
[self.window addSubview:firstViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
第一个视图切换按钮响应事件
- (IBAction)switchView:(id)sender {
FirstViewController *firstController = [[FirstViewController alloc] init];
[self.navigationController pushViewController:firstController animated:YES];
firstController.title = @"第一个视图另一个视图";
}
第四个视图添加两个按钮方法,在最后一个控制机的.m文件中的-(void)viewDidLoad方法中
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"测试"
style:UIBarButtonItemStylePlain target:self
action:@selector(pullWarn)];
self.navigationItem.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
}