首页 > 其他分享 >UITabBarController使用

UITabBarController使用

时间:2023-03-30 20:03:49浏览次数:33  
标签:alloc viewController self item init 使用 UITabBarController view



[cpp] 
     view plain
     copy
     print
     ?
    
 
   
1. {  
2.    if ([super init] != nil) {  
3.        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"asdfsadf" image:[UIImage imageNamed:@"WWAN5.png"] tag:1];  
4.        self.tabBarItem = item;  
5.        [item release];  
6.    }  
7.    return self;  
8. }


-(id)init { if ([super init] != nil) { UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"asdfsadf" image:[UIImage imageNamed:@"WWAN5.png"] tag:1]; self.tabBarItem = item; [item release]; } return self; } 我很少写关于IOS的文章,写这篇完全是因为网络上copy,paste的文章太多,将我误导,搞的我花了半天时间才会用这控件,最后还是看了外国一个英文贴子,才会用。因此写了这篇供后学之人学习加快吧,也希望大家在写文章时,不要千篇一律的复制、粘贴。我们是软件工程师,而不是复制、粘贴工程师。

该文章内容展示效果如下图:

UITabBarController使用_控件

接下来,你将看到完全用代码实现的tab bar选项卡切换效果。下面开始。

准备工作,创建一个项目名为ViewSwitcher(你可以选择基于View-based Application或Window-based Application,只不过选择后者的话,要自己创建一个view controller罢了,我是选择了第一个)。

在此,我不会教大家只在ViewSwitcherAppDelegate中去创建UITabBarController的实例(这种方式网上到处都是),我要教大家如何在自己的view controller中创建UITabBarController实例。

下一步,创建两个view controller类,我这里命名为BlueViewController和YellowViewController,当然,它们都是UIViewController的子类。

er的viewDidLoad方法中,代码如下:


[cpp] 
     view plain
     copy
     print
     ?
    
 
   
1. = [[UITabBarController alloc] init];  
2. = self;  
3. = [[BlueViewController alloc] init];  
4. = [[YellowViewController alloc] init];  
5. *viewControllerArray = [NSArray arrayWithObjects:blueViewController,yellowViewController,nil];  
6. = viewControllerArray;  
7. = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);  
8. addSubview:tabBar.view];  
9. release];  
 
  tabBar = [[UITabBarController alloc] init];
        tabBar.delegate = self;
        blueViewController = [[BlueViewController alloc] init];
        yellowViewController = [[YellowViewController alloc] init];
        NSArray *viewControllerArray = [NSArray arrayWithObjects:blueViewController,yellowViewController,nil];
        tabBar.viewControllers = viewControllerArray;
        tabBar.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        [self.view addSubview:tabBar.view];
        [viewControllerArray release];

其中tabBar是在.h文件中声明的UITabBarController对象实例。这样运行看看吧。

你会看到为什么两个按钮是黑色的呢,没有字呢?没错,因为我们还没有写这部分代码。设置tab bar标签的图片或文字,可以在它的子view controller中做(这么说或许不是很恰当,因为官方可不这么叫),在这里,我是写在blueViewController和yellowViewController中的,重写它们的init方法,将它们的tabBarItem成员赋值,代码如下:



[css] 
     view plain
     copy
     print
     ?
    
 
   
1. {  
2.    if ([super init] != nil) {  
3.        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"asdfsadf" image:[UIImage imageNamed:@"WWAN5.png"] tag:1];  
4.        self.tabBarItem = item;  
5.        [item release];  
6.    }  
7.    return self;  
8. }


-(id)init { if ([super init] != nil) { UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"asdfsadf" image:[UIImage imageNamed:@"WWAN5.png"] tag:1]; self.tabBarItem = item; [item release]; } return self; }  

运行进来 ,你将看到新的效果。

那么,那个在item上的小红圈提示是怎么来的呢??我们实现UITabBarDelegate中的- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController方法,代码如下:


[cpp] 
     view plain
     copy
     print
     ?
    
 
   
1. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{  
2. //  [self.view addSubview:viewController.view];   
3.    //  tabBarController.selectedViewController = viewController;   
4.    viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",80];  
5. //  viewController.tabBarItem.title = @"aaa";   
6. }  
 
  - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//      [self.view addSubview:viewController.view];
        //      tabBarController.selectedViewController = viewController;
        viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",80];
//      viewController.tabBarItem.title = @"aaa";
}

聪明的人,应该不需要我一行行的去讲解代码吧!

最后,转载请注明出处,谢谢!

标签:alloc,viewController,self,item,init,使用,UITabBarController,view
From: https://blog.51cto.com/u_548275/6160137

相关文章

  • 关于android分辨率和使用iphone版…
    为了节省成本,开发app,一般情况下android和iphone用的是一套设计图。那适配怎么办?今天统计了下android的分辨率主要有:三星i9300 galaxys3 1280x720像素三星galaxys4  1920x1080像素还有经典的分辨率:854x480 还有部分960x540的。他们有个共同的特点,就是高宽比都是 1.......
  • C#使用Java的秘钥对进行SHA256withRSA签名验签
    usingOrg.BouncyCastle.Crypto.Parameters;usingOrg.BouncyCastle.Security;usingSystem;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Xml;///<summary>///RSA私钥,从Java格式转.net格式(不依赖第三方包)///</summary>//......
  • 如何使用 MegEngine 生态落地一个算法
    在当今人工智能领域,深度学习算法已经广泛应用于图像处理、自然语言处理、语音识别等各种领域。然而,实现一个高效的深度学习算法需要运用大量的技术和工具,并要面临着许多挑战,如训练计算资源消耗大,模型转换难,高效快捷的推理部署等问题。MegEngine作为一个训推一体的深度学习框架,其......
  • Raphaël 使用 Animation 绘制沿线条移动的圆
    使用Raphael绘制图形时,可以绘制一个圆点沿着指定线条路径进行移动,可以使用Raphael的 Paper.customAttributes增加处理方法和animation进行动画定义。只需要指定正确的raphael.js路径,以下代码就可以运行:<!DOCTYPEhtml><htmllang="en"><head><metacharset="utf-8"><tit......
  • Jtti:如何使用云服务器防火墙保障数据安全?
    云服务器防火墙是保障云计算数据安全的关键措施之一,但是一些安全隐患也需要我们注意。本文将从以下角度探讨如何使用云服务器防火墙保障数据安全。1.防火墙漏洞防火墙程序也可能存在漏洞,影响防护效果。针对漏洞需要及时修复或更新防火墙程序,加强安全保障。2.网络配置不当网络配置不......
  • gdb调试工具使用
    gdb调试工具使用首先使用gcc和-g参数生成含有调试信息的可执行程序,-Wall参数输出warnning,避免未知错误gccxxx.c-oxxx-g-Wallgdb使用参数如下gdb断点操作......
  • inotifywait基本使用
    开始之前需要检测系统内核是否支持inotify方式一:使用uname-r命令检查Linux内核,如果低于2.6.13,就需要重新编译内核加入inotify的支持。方式二:使用ll/proc/sys/fs/inotify命令,是否有以下三条信息输出,如果没有表示不支持。-rw-r--r--1rootroot0Mar1615:43max_queued_e......
  • webpack的基本使用(八)CleanWebpackPlugin
    为了打包之前把dist底下的文件先删除了先,然后再进行打包,我们使用clean-webpack-plugin安装  配置 将构造函数放入webpackPlugin中进行  ......
  • Django admin中TabularInline和StackedInline使用
    TabularInline类用于在A页面上编辑B表的字段,举例说明,如果models.py中有Staff、Job两个model:fromdjango.dbimportmodelsclassStaff(models.Model):name=model......
  • 1.celery的基本使用
    一、什么是Celery1.1、celery是什么Celery是一个简单、灵活且可靠的,处理大量消息的分布式系统,专注于实时处理的异步任务队列,同时也支持任务调度。   Celery的架构......