最后效果图:

main.storyboard

BeyondViewController.m中有一句关键代码,设置tabbarItem图片的样式(30*30)

//
// BeyondViewController.m
// 17_控制器切换2_tabbarController
//
// Created by beyond on 14-7-31.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "BeyondViewController.h"
#import "NanaViewController.h"
#import "SettingViewController.h"
@interface BeyondViewController () @end @implementation BeyondViewController - (void)viewDidLoad
{
[super viewDidLoad]; NSLog(@"view did loaded"); // UITabbarController里面的tabbarItem中的图片须要特殊API处理之后,才干正常显示
UIImage *img = [UIImage imageNamed:@"home"];
UIImage *img_selected = [UIImage imageNamed:@"home_s"];
// 设置图片 渲染 模式
img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 设置图片 渲染 模式
img_selected = [img_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 构造方法生成 UITabBarItem
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:img selectedImage:img_selected]; // 设置当前控制器的 tabBarItem属性
self.tabBarItem = item;
self.tabBarItem.title = @"首页";
self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",1] ; // 默认情况下,app执行之后,仅仅执行第一个控制器的view did load方法,因此,要得到UITabBarController容器中全部的控制器,调用其自己定义的一个方法,设置它们自己的tabbaritem样式 // 先得到父容器UITabBarController,有了它,就有了全部的控制器实例的引用
UITabBarController *parentCtrl = self.parentViewController;
// 得到容器UITabBarController中全部的子控制器
NSArray *children = [parentCtrl childViewControllers];
// 调用相应的子控制器的 自己定义方法,设置它们自己的tabbaritem样式
NanaViewController *nanaVC = (NanaViewController *)[children objectAtIndex:1];
[nanaVC setTabBarItemDIY]; // 调用相应的子控制器的 自己定义方法,设置它们自己的tabbaritem样式
SettingViewController *setVC = (SettingViewController *)[children objectAtIndex:2];
[setVC setTabBarItemDIY]; } @end

NanaViewController.h

//
// NanaViewController.h
// 17_控制器切换2_tabbarController
//
// Created by beyond on 14-7-31.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import <UIKit/UIKit.h> @interface NanaViewController : UIViewController // 自己定义方法,设置自己的tabbaritem样式
- (void) setTabBarItemDIY;
@end

NanaViewController.m

//
// NanaViewController.m
// 17_控制器切换2_tabbarController
//
// Created by beyond on 14-7-31.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "NanaViewController.h" @interface NanaViewController () @end @implementation NanaViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"view did loaded 2"); }
// 自己定义方法,设置自己的tabbaritem样式
- (void) setTabBarItemDIY
{
// UITabbarController里面的tabbarItem中的图片须要特殊API处理之后,才干正常显示
UIImage *img = [UIImage imageNamed:@"nana"];
UIImage *img_selected = [UIImage imageNamed:@"nana_s"];
// 设置图片 渲染 模式
img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 设置图片 渲染 模式
img_selected = [img_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 构造方法生成 UITabBarItem
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:img selectedImage:img_selected]; // 设置当前控制器的 tabBarItem属性
self.tabBarItem = item;
self.tabBarItem.title = @"娜娜";
self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",2] ;
} @end

SettingViewController.h

//
// SettingViewController.h
// 17_控制器切换2_tabbarController
//
// Created by beyond on 14-7-31.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import <UIKit/UIKit.h> @interface SettingViewController : UIViewController // 自己定义方法,设置自己的tabbaritem样式
- (void) setTabBarItemDIY;
@end

SettingViewController.m

//
// SettingViewController.m
// 17_控制器切换2_tabbarController
//
// Created by beyond on 14-7-31.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import "SettingViewController.h" @interface SettingViewController () @end @implementation SettingViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"view did loaded 3"); } // 自己定义方法,设置自己的tabbaritem样式
- (void) setTabBarItemDIY
{
// UITabbarController里面的tabbarItem中的图片须要特殊API处理之后,才干正常显示
UIImage *img = [UIImage imageNamed:@"setting"];
UIImage *img_selected = [UIImage imageNamed:@"setting_s"];
// 设置图片 渲染 模式
img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 设置图片 渲染 模式
img_selected = [img_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 构造方法生成 UITabBarItem
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"" image:img selectedImage:img_selected]; // 设置当前控制器的 tabBarItem属性
self.tabBarItem = item;
self.tabBarItem.title = @"我的";
self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",3] ;
}
@end

版权声明:本文博客原创文章。博客,未经同意,不得转载。

iOS_17_控制开关_TabBarController_由storyboard道路的更多相关文章

  1. 免费道路 bzoj 3624

    免费道路(1s 128MB)roads [输入样例] 5 7 21 3 04 5 13 2 05 3 14 3 01 2 14 2 1 [输出样例] 3 2 04 3 05 3 11 2 1 题解: ...

  2. LaunchScreen.storyboard启动图遇到的坑

    Xcode有时候在LaunchScreen.storyBoard中修改了启动图片之后,运行却没有效果,直接白屏,而往storyboard中拖插件是可以显示的,设置成Assets.xcassets中的其 ...

  3. iOS系列 基础篇 02 StoryBoard 故事板文件

    iOS基础 02 StoryBoard 故事板文件 目录: 1. 故事板的导航特点 2. 故事板中的Scene和Segue 3. 本文最后 在上篇HelloWorld工程中有一个Main.storyb ...

  4. Xcode6新特性(1)-删除Main.storyboard

    当新建完一个空项目的时候,Xcode会自动创建一个Main.storyboard的空文件,如果不需要,可以将其删除.但是如果删除,再次运行程序,程序会报错,提示找不到Main.storyboard文件 ...

  5. Storyboard & XIB 自己的理解

    //1.storyboard //(1) //此处bundle:nil 等价于 [NSBundle mainBundle] //    SecondViewController *secondVc = ...

  6. 控制Storyboard播放zz

    <Grid Width="300" Height="460"> <Grid.RowDefinitions> <RowDefinit ...

  7. 代码中使用StoryBoard和DoubleAnimation的方法

    TranslateTransformを対象に.DoubleAnimation型のアニメーションを使用して.TranslateTransform.Xプロパティを ”-1 * Imageコントロールの幅” ...

  8. iOS开发之使用Storyboard预览UI在不同屏幕上的运行效果

    在公司做项目一直使用Storyboard,虽然有时会遇到团队合作的Storyboard冲突问题,但是对于Storyboard开发效率之高还是比较划算的.在之前的博客中也提到过,团队合作使用Storyb ...

  9. IOS开发之获取Storyboard创建的ViewController

    前面的两篇博客都是学习有关屏幕适配也就是相对布局的东西,这篇博客中将会学习视图间的切换.视图间的切换我们可以用代码完成或者用storyboard来建立各个视图控制器间的关系.在需要用到代码进行切换时会 ...

随机推荐

  1. Domain Model(领域模型)

    Domain Model(领域模型) 上一篇:<DDD 领域驱动设计-如何 DDD?> 开源地址:https://github.com/yuezhongxin/CNBlogs.Apply. ...

  2. PHP监測memcache服务端的执行状况

    . 代码例如以下,代码为memcache官方代码,引用在此.做一下简单的说明: 1.设置username和password define('ADMIN_USERNAME','admin'); // A ...

  3. AIX常用命令略记

    ■ 初始化端末时可能需要确认服务器端和端末时间是否匹配 ●cal 显示日历 ●date 显示服务前当前时间 ■    显示当前目录,即显示当前所在目录的adress ●pwd(print workin ...

  4. Python 类继承,__bases__, __mro__, super

    Python是面向对象的编程语言,也支持类继承. >>> class Base: ... pass ... >>> class Derived(Base): ... ...

  5. JavaScript RegExp对象

    一.什么是RegExp         1.RegExp 是正則表達式的缩写. 2.当您检索某个文本时,能够使用一种模式来描写叙述要检索的内容.RegExp 就是这样的模式. 3.简单的模式能够是一个 ...

  6. SWT中的Tree中 添加右键弹出菜单

    先看一下效果: 如图:在树上单击鼠标右键会弹出 弹出式菜单.做法其实很简单,先做一个树: final TreeViewer treeViewer = new TreeViewer(group, SWT ...

  7. poj1655(树形dp)

    题目链接:http://poj.org/problem?id=1655 题目大意:给一个树,删除其中一个点就会形成一个森林,点的平衡度为删除了这个节点后,所形成多个树,其中组成树的节点最多,节点个数就 ...

  8. BC 2015在百度之星程序设计大赛 - 预赛(1)(KPI-树董事长)

    KPI Accepts: 517 Submissions: 2185 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...

  9. WebApi异常

    WebApi异常处理解决方案   前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定 ...

  10. 【android自己定义控件】自己定义View属性

    1.自己定义View的属性 2.在View的构造方法中获得我们自己定义的属性 3.重写onMesure 4.重写onDraw 3这个步骤不是必须,当然了大部分情况下还是须要重写的. 1.自己定义Vie ...