IOS:标签栏与导航栏
来源:互联网 发布:数控铣简单图形编程 编辑:程序博客网 时间:2024/06/01 09:20
1、在 UIKit 中UITabbar 代表了标签栏,而 UITabBarController 对其进行了封装,令多个不同的视图管理与切换变的更加轻松。构建一个标签栏控制器,首先要为每个按钮准备一个单独的页。每一页都应被创建为UIViewController对象。
首先创建一个单视图工程,然后在建两个新类,他们都继承自UIViewController,分别取名为firstViewController,secondViewController。
在AppDelegate.h中代码如下:
import <UIKit/UIKit.h>
#import "firstViewController.h"
#import "secondViewController.h"
@class ViewController;
@interface AppDelegate :UIResponder <UIApplicationDelegate>
@property (strong,nonatomic)UIWindow *window;
@property (strong,nonatomic)ViewController *viewController;
@property(retain,nonatomic)firstViewController *pFirst;
@property(retain,nonatomic)secondViewController *pSecond;
@end
//创建初始化视图控制器
firstViewController *pfirstVC = [[firstViewControlleralloc]initWithNibName:nilbundle:nil];
secondViewController *psecondVC = [[secondViewControlleralloc]initWithNibName:nilbundle:nil];
self.pFirst = pfirstVC;
self.pSecond = psecondVC;
[pfirstVCrelease];
[psecondVCrelease];
//创建UITabBarController对象
UITabBarController *pBar = [[UITabBarControlleralloc]init];
//创建数组来存放多个试图控制器对象
NSArray *mArr = [NSArrayarrayWithObjects:self.pFirst,self.pSecond,nil];
//将UITabBarController对象的viewControllers设置为数组里的元素
pBar.viewControllers = mArr;
//将当前窗口的根视图控制器设为pBar
self.window.rootViewController = pBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil方法中添加如下代码:
//设置标签栏的格式和tag
self.tabBarItem = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarkstag:10];
//设置视图背景色为红色
self.view.backgroundColor = [UIColorredColor];
//设置标签栏的标题和背景图片(名字为“10.png”)以及tag
self.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"设置"image:[UIImageimageNamed:@"10.png"] tag:20];
//设置视图背景色为黄色
self.view.backgroundColor = [UIColoryellowColor];
最后,运行结果:
//创建初始化导航控制器
UINavigationController *pNavigation = [[UINavigationControlleralloc]initWithRootViewController:self.viewController];
//将导航控制器对象设为窗口的根视图控制器
self.window.rootViewController = pNavigation;
//释放
[pNavigationrelease];
//设置导航栏标题
self.navigationItem.title =@"导航栏";
//设置导航栏上的按钮
UIBarButtonItem *pBtn = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(buttonAction:)];
//将按钮添加到导航栏右边
self.navigationItem.rightBarButtonItem = pBtn;
//按钮关联的动作
- (void)buttonAction:(id)sender
{
UIAlertView *pAlert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"You can breath easy!"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];
[pAlertshow];
}
UINavigationController *pNavi1 = [[UINavigationControlleralloc]initWithRootViewController:self.pFirst];
UINavigationController *pNavi2 = [[UINavigationControlleralloc]initWithRootViewController:self.pSecond];
//创建UITabBarController对象
UITabBarController *pBar = [[UITabBarControlleralloc]init];
//创建数组来存放多个试图控制器对象
NSArray *mArr = [NSArrayarrayWithObjects:pNavi1,pNavi2,nil];
//将UITabBarController对象的viewControllers设置为数组里的元素
pBar.viewControllers = mArr;
//将当前窗口的根视图控制器设为pBar
self.window.rootViewController = pBar;
运行结果
可以看到,有导航栏生成,然后我们可以分别在每个类中对的导航类进行设置,即可。
self.tabBarItem.badgeValue =@"8";
- IOS:标签栏与导航栏
- iOS 标签栏和导航栏
- IOS 自定义导航栏和标签栏
- iOS学习笔记——导航栏与标签栏结合
- 标签导航栏PagerSlidingTabStrip
- ios 导航栏,标签栏,工具栏和状态栏
- iOS -- 导航栏和标签栏的一些设置
- iOS导航控制器和标签栏控制器的结合
- iOS导航栏,标签视图的透明度处理
- iOS与导航栏有关的神技
- iOS--UI之导航控制器与标签控制器
- IOS导航栏UINavigation
- ios 导航栏风格
- ios 导航栏显示
- IOS-导航栏风格
- ios 导航栏设置
- iOS 导航栏
- ios顶部导航栏
- 按钮的拖拽复制
- 使用Shell脚本对Linux系统和进程资源进行监控
- 通过java的反射编写泛型数组
- maven几个知识点
- 浅谈c#中的泛型(一)
- IOS:标签栏与导航栏
- CentOS 环境配置积累
- Java(Android)写文件时指定编码格式的问题
- 发布网站,报Access to the path is denied的解决办法
- oc学习之旅:NSFileHandle与NSFileManager实现FileCopy
- 欢迎您在新浪博客安家
- “二调”全解读
- php 搜索工作经验小结
- 【转】MapGIS基础系列(三)…