UI基础整理-13

来源:互联网 发布:mac如何切换全角半角 编辑:程序博客网 时间:2024/06/03 20:32

block&单例&tabBarController





block

声明 : 

.h                    

 typedef void (^Block)(NSString *string);

@interface CustomViewController : UIViewController

//MRC下使用copy,passValueBlock从栈区拷贝到堆区

//ARC下使用strong完成拷贝

@property (nonatomic ,strong)Block passValueBlock;

//strong自带拷贝的功能    block写成属性                     

@end


调用 : 

.m

self.passValueBlock(self.textField.text);

实现 :

收值得界面  

    //调用block时的代码实现

customVC.passValueBlock = ^(NSString *string){

        self.label.text = string;

 };


单例(不完整单例)

使用类方法创建

在页面中使用方法创建对象的时候,会走写的类方法(判断是否存在,如果存在就是用原先的,如果不存在,就创建一个新的)


//只能使用一次,其他的方法也能识别

//放入静态区

static SHSingleton *singleton = nil;


+ (instancetype)shareSingleton{

    //唯一的

    if(nil == singleton){

        

        singleton = [[SHSingleton alloc]init];

    }

    

    return singleton;

}


tabBarController

//

//  AppDelegate.m

//  Lesson13_tabBarController

//

//  Created by Floating_SH on 15/12/3.

//  Copyright © 2015 SH. All rights reserved.

//


#import "AppDelegate.h"



@interface AppDelegate ()<UITabBarControllerDelegate>

@property (nonatomic ,strong)UITabBarController *tabBC;

@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    

    self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

    [self.windowsetBackgroundColor:[UIColorwhiteColor]];

    [self.windowmakeKeyAndVisible];

    

    //创建子控制器

    FirstViewController *firstVC = [[FirstViewControlleralloc]init];

    UINavigationController *firstNC = [[UINavigationControlleralloc]initWithRootViewController:firstVC];

    

    SecondViewController *secondVC = [[SecondViewControlleralloc]init];

    UINavigationController *secondNC = [[UINavigationControlleralloc]initWithRootViewController:secondVC];

    ThirdViewController *thirdVC = [[ThirdViewControlleralloc]init];

    UINavigationController *thirdNC = [[UINavigationControlleralloc]initWithRootViewController:thirdVC];

    ForthViewController *forthVC = [[ForthViewControlleralloc]init];

    UINavigationController *forthNC = [[UINavigationControlleralloc]initWithRootViewController:forthVC];

    FifthViewController *fifthVC = [[FifthViewControlleralloc]init];

    UINavigationController *fifthNC = [[UINavigationControlleralloc]initWithRootViewController:fifthVC];


    

    //创建标签视图控制器

    self.tabBC = [[UITabBarControlleralloc]init];

    //设置子控制器

    self.tabBC.viewControllers =@[firstNC,secondNC,thirdNC,forthNC,fifthNC];

    

    //设置title

//    firstVC.title = @"第一页";

//    secondVC.title = @"第二页";

//    thirdVC.title = @"第三页";

//    forthVC.title = @"第四页";

//    fifthVC.title = @"第五页";

//    sixthVC.title = @"第六页";

//    seventVC.title = @"第七页";

    //设置默认选中下标

    self.tabBC.selectedIndex =3;

    //代理

    self.tabBC.delegate =self;

    

    //badgeValue

    firstVC.tabBarItem.badgeValue =@"99+";

    

    //tabBar默认高度是49

    

    

    //tarBar选中时字体的颜色

    self.tabBC.tabBar.tintColor = [UIColororangeColor];

    //tabBar颜色

//    self.tabBC.tabBar.barTintColor = [UIColor redColor];

    

    //tarBar样式(慎用)

//    self.tabBC.tabBar.barStyle = UIBarStyleBlackOpaque;

    //设置背景图片

//    self.tabBC.tabBar.backgroundImage = [UIImage imageNamed:@"1"];

//    UIImage *image = [UIImage imageNamed:@"1"];

//    image = [image imageWithAlignmentRectInsets:(UIEdgeInsetsMake(0, 20, 20, 20))];

//    self.tabBC.tabBar.backgroundImage = image;

    

 

    //设置为window的根视图控制器

    self.window.rootViewController =self.tabBC;

    

    

    return YES;

}

#pragma mark -------------tabBarControllerDelegate------------

//已经选中某一个视图控制器 :通常情况下进行视图的设置

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

    

    //子类指针无法指向父类对象,使用强转

    //viewController: 我们选中的控制器

    if (tabBarController.selectedIndex ==1) {

        

        SecondViewController *secondVC = (SecondViewController *)viewController;

    }

    //点第一个页面的时候,99+消失

    if(tabBarController.selectedIndex ==0){

        FirstViewController *firstVC = (FirstViewController *)viewController;

        firstVC.tabBarItem.badgeValue =nil;

        

    }

    NSLog(@"%s",__FUNCTION__);

}





//将要选中某一个视图控制器 :通常情况下进行数据的准备

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

    

    NSLog(@"%s",__FUNCTION__);

    return YES;

}


0 0
原创粉丝点击