开发者社区> 社会主义> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

UIKit 框架之Bar、Controller

简介:
+关注继续查看

UIKit框架中有各种Bar,UITabBar、UINavigationBar、UIToolbar。Bar对应的就有一些Item,tabBarItem、navigationItem、toolbarItems,再加上UIViewController、UINavigationController、UITabBarController很容易搞糊涂。我看了好久,没看明白。动手敲了下才有一点感觉。

一、联系

一个UINavigationController对应着一个UINavigationBar、UIToolbar,UIToolbar默认不显示,toolbarHidden=NO时显示。一个UITabBarController对应着一个UITabBar.一个UIViewController对应着一个tabBarItem、navigationItem和多个toolbarItems。navigationItem中可以设置左右按钮和中间视图等。

二、代码demo

1.首先代码结构 viewController1-viewController5用于生成5个tabBarItem。viewController、viewController6主要模拟登录注册,有时候需要先让用户登录注册之后才能进入。

2.代码

1.在AppDelegate.m中


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    tabBarViewController *tabVc=[[tabBarViewController alloc]init];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor=[UIColor whiteColor];
    self.window.rootViewController=tabVc;
    return YES;
}

 2.ViewController.m


//
//  ViewController.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//
 
#import "ViewController.h"
#import "ViewController6.h"
 
@interface ViewController ()
 
@end
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@ %@",NSStringFromCGRect(self.tabBarController.tabBar.frame),NSStringFromCGRect(self.navigationController.toolbar.frame));
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame=CGRectMake(100, 100, 100, 100);
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:nil];
    self.toolbarItems=@[item1];
}
-(void)btnClick:(id)sender
{
    ViewController6 *vc6=[[ViewController6 alloc]init];
    vc6.title=@"第6页";
    vc6.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc6 animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

 3.ViewController1.m


//
//  ViewController1.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//
 
#import "ViewController1.h"
#import "ViewController2.h"
 
@interface ViewController1 ()
 
@end
 
@implementation ViewController1
 
- (void)viewDidLoad {
    [super viewDidLoad];
   
     
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

 4.ViewController2.m


//
//  ViewController2.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//
 
#import "ViewController2.h"
#import "ViewController.h"
#import "navigationViewController.h"
 
@interface ViewController2 ()
 
@end
 
@implementation ViewController2
 
- (void)viewDidLoad {
    [super viewDidLoad];
    ViewController *vc=[[ViewController alloc]init];
//    navigationViewController *nav=[[navigationViewController alloc]initWithRootViewController:vc];
//    self.navigationController.toolbarHidden=NO;
//    nav.toolbarHidden=NO;
    [self.navigationController addChildViewController:vc];
     
 
}
 
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end

 5.ViewController6.m


//
//  ViewController6.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//
 
#import "ViewController6.h"
 
@interface ViewController6 ()
 
@end
 
@implementation ViewController6
 
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@ %@",NSStringFromCGRect(self.tabBarController.tabBar.frame),NSStringFromCGRect(self.navigationController.toolbar.frame));
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(btnClick:)];
    self.toolbarItems=@[item1];
}
-(void)btnClick:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end

 6.tabBarViewController.m继承UITabBarController


//
//  tabBarViewController.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//
 
#import "tabBarViewController.h"
#import "ViewController.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
#import "ViewController5.h"
#import "navigationViewController.h"
 
@interface tabBarViewController ()<UITabBarControllerDelegate>
 
@end
 
@implementation tabBarViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self steup];
    self.delegate=self;
    self.selectedIndex=2;
    self.moreNavigationController.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:1001];
    self.moreNavigationController.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:nil];
    self.moreNavigationController.visibleViewController.navigationItem.title=@"更多功能";
    self.moreNavigationController.visibleViewController.navigationItem.rightBarButtonItem.title=@"编辑";
 
}
-(void)rightClick:(id)sender
{
     
}
-(void)steup
{
    ViewController *vc=[[ViewController alloc]init];
    vc.title=@"第0页";
    navigationViewController *nvc=[[navigationViewController alloc]initWithRootViewController:vc];
    nvc.toolbarHidden=NO;
    UITabBarItem *tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第0页" image:[UIImage imageNamed:@"tabbar_homepage_normal"] selectedImage:[UIImage imageNamed:@"tabbar_homepage_selected"]];
    tabBarItem.badgeValue=@"2";
    nvc.tabBarItem=tabBarItem;
     
    ViewController1 *vc1=[[ViewController1 alloc]init];
    vc1.title=@"第1页";
    navigationViewController *nvc1=[[navigationViewController alloc]initWithRootViewController:vc1];
    nvc1.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第1页" image:[UIImage imageNamed:@"tabbar_convenience_normal"] selectedImage:[UIImage imageNamed:@"tabbar_convenience_selected"]];
     
    ViewController2 *vc2=[[ViewController2 alloc]init];
    vc2.title=@"第2页";
     
    navigationViewController *nvc2=[[navigationViewController alloc]initWithRootViewController:vc2];
    nvc2.toolbarHidden=NO;
    nvc2.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第2页" image:[UIImage imageNamed:@"tabbar_electrice_commerce_normal"] selectedImage:[UIImage imageNamed:@"tabbar_electrice_commerce_selected"]];
     
     
    ViewController3 *vc3=[[ViewController3 alloc]init];
    vc3.title=@"第3页";
    navigationViewController *nvc3=[[navigationViewController alloc]initWithRootViewController:vc3];
    vc3.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第3页" image:[UIImage imageNamed:@"tabbar_me_normal"] selectedImage:[UIImage imageNamed:@"tabbar_me_selected"]];
     
    ViewController4 *vc4=[[ViewController4 alloc]init];
    vc4.title=@"第4页";
    navigationViewController *nvc4=[[navigationViewController alloc]initWithRootViewController:vc4];
     nvc4.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第4页" image:[UIImage imageNamed:@"tabbar_convenience_normal"] selectedImage:[UIImage imageNamed:@"tabbar_convenience_selected"]];
     
    ViewController5 *vc5=[[ViewController5 alloc]init];
    vc5.title=@"第5页";
    navigationViewController *nvc5=[[navigationViewController alloc]initWithRootViewController:vc5];
     nvc5.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"第5页" image:[UIImage imageNamed:@"tabbar_homepage_normal"] selectedImage:[UIImage imageNamed:@"tabbar_homepage_selected"]];
    // 默认的顺序
     NSArray *defaultarr=@[nvc,nvc1,nvc2,nvc3,nvc4,nvc5];
    // 自定义的顺序
     NSMutableArray *newarr=[[NSMutableArray alloc]init];
    //获取保存的title数组
    NSArray *titles=[[NSUserDefaults standardUserDefaults] arrayForKey:@"vcs"];
    // 第一次启动为nil
    if (titles==nil) {
        newarr=[defaultarr copy];
    }
    //根据自定义title数组 设置newarr数组
    for (NSString *s in titles) {
        for (navigationViewController *nvc in defaultarr) {
            if ([s isEqualToString:nvc.visibleViewController.title]) {
                [newarr addObject:nvc];
            }
        }
    }
    self.viewControllers=newarr;
     
     
}
//UITabBarControllerDelegate
//是否可以选中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    return YES;
}
//选中某个viewController
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//    NSLog(@"%@",viewController.title);
}
//开始自定义viewControllers
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
    NSLog(@"%@",viewControllers);
}
//即将编辑结束
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    NSLog(@"%d",changed);
}
//编辑结束
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    //记下选择的选项顺序,方便下次启动时显示
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    if (!changed) {
        return;
    }
    for (UIViewController *vc in viewControllers) {
        [arr addObject:vc.title];
    }
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:arr forKey:@"vcs"];
    
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
@end

 7.navigationViewController.m


//
//  navigationViewController.m
//  UITabBarController
//
//  Created by City--Online on 15/5/26.
//  Copyright (c) 2015年 XQB. All rights reserved.
//
 
#import "navigationViewController.h"
 
@interface navigationViewController ()<UINavigationControllerDelegate>
 
@end
 
@implementation navigationViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
//    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
//    {
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//    }
    self.navigationController.navigationBar.translucent=NO;
    self.delegate=self;
     
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSLog(@"%@",viewController.title);
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
@end

 实现效果:


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Kafka中的Controller(控制器)节点
Kafka中的Controller(控制器)节点
82 0
SAP WebClient UI view controller所有可用的属性列表
SAP WebClient UI view controller所有可用的属性列表
28 0
SAP UI5应用入口App.controller.js是如何被UI5框架加载的?
首先在UI5应用的manifes.json里,定义了UI5应用的入口视图为App:
36 0
SAP UI5应用入口App.controller.js是如何被UI5框架加载的?
首先在UI5应用的manifes.json里,定义了UI5应用的入口视图为App: 调试器里的pending数组的两个元素: 实际上对应了我在App.controller.js里定义的两个依赖: 而aModules数组里的两个对象,就是BaseController和JSONModel模块加载后的实例: 有经验的开发人员从这些函数列表bindProperty, bindTree, dataLoaded,getJSON等就能得知这个实例是JSONModel的实现。
530 0
+关注
社会主义
从.Net到iOS,在撸的道路上越走越远,工作之余经营着博客园http://www.cnblogs.com/5ishare,欢迎小伙伴(妹子更好)一起交流,谈谈人生理想。作为经常从网上索取免费资料的一员,要有回报回报的思想,也让更多的人少走弯路.
237
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载