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

  

   在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代码:
 

//创建初始化视图控制器

    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;

    

   
    然后,我们可以在两个两类中分别对标签栏进行自定义设置,如在firstViewController.m中的

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil方法中添加如下代码:

  

//设置标签栏的格式和tag

        self.tabBarItem = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarkstag:10];

        //设置视图背景色为红色

        self.view.backgroundColor = [UIColorredColor];

     

    同样,在secondViewController.m中的-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil方法中添加如下代码:

 //设置标签栏的标题和背景图片(名字为“10.png”)以及tag

        self.tabBarItem = [[UITabBarItemalloc]initWithTitle:@"设置"image:[UIImageimageNamed:@"10.png"] tag:20];

        //设置视图背景色为黄色

        self.view.backgroundColor = [UIColoryellowColor];


  最后,运行结果:




  点击“设置”按钮,视图会转换,
   
  
      
  
  2、导航栏

   同样创建单视图工程,在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代码:

  

 //创建初始化导航控制器

    UINavigationController *pNavigation = [[UINavigationControlleralloc]initWithRootViewController:self.viewController];

    //将导航控制器对象设为窗口的根视图控制器

   self.window.rootViewController = pNavigation;

   //释放

    [pNavigationrelease];

    

    然后,我们可以在ViewController.m中的- (void)viewDidLoad添加如下代码
   

//设置导航栏标题

    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];

}


     运行结果:


  

      点击“Done”按钮,结果如下:

  
      点击"OK"回返会上一个界面。
  
  3、要将导航栏和标签栏结合起来只需要在我们创建标签栏的代码上添加并改动几句代码,如下
   

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;


  运行结果

   


  可以看到,有导航栏生成,然后我们可以分别在每个类中对的导航类进行设置,即可。



 

   另外,我们经常可以看到在标签栏上有一个包含数字的小红圈,如图:

   

  这是tabBarItem的badValue属性,只需要在初始化方法添加一句话即可实现:

    

        self.tabBarItem.badgeValue =@"8";





0 0
原创粉丝点击