看到很多项目中都采用的是Navigation加Tab Bar组合到一起,完成视图切换操作,在导航栏上添加基本按钮,给予响应时间,让应用给用户更好的体验,所以本菜鸟写了这个这样一个Demo,仅供学习
所创建工程模板是最后一个 Empty Application
先看运行效果:
第一个视图,点击按钮切换视图,点击导航栏上按钮可以切换回去
第二个视图设置了背景颜色和透明度 第三个视图添加了背景图片
第四个视图,在导航栏上添加了两个按钮,左边按钮属于自定义标题,右边按钮是系统的图标,点击左按钮弹出一个警告,右边按钮没有添加响应事件,点击后没反应
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; }
源代码:http://download.csdn.net/detail/duxinfeng2010/4576308