随意细解:UI -- 标签视图控制器

UITabBarController

标签视图控制器

这里写图片描述

常用属性

  • 显示的视图控制器(需要添加到viewControllers的数组中)

    self.viewControllers = @[oneNav, twoNav, threeNav, fourNav, fiveNav, sixNav];
  • 标签栏 – tabBar

    // 设置tabBar的颜色
    self.tabBar.barTintColor = [UIColor whiteColor];
    self.tabBar.tintColor = [UIColor blackColor];
    // tabBar的高度  : 49 
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabBar"];
  • 设置默认选中的页面

    self.selectedIndex = 2;  //(默认为0
  • tabBarItem上的红色提示按钮

    twoVC.tabBarItem.badgeValue = @"2";

代理方法

首先要设置代理

  • 可以指定哪个控制器不让点击

     - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
    {
    // 取出不让点击的控制器
    UINavigationController *nav = self.viewControllers[3];
    // 如果选中的控制器是你不想让用户点击的
    // 那么返回NO
    if (viewController == nav) {
        return NO;
    }else{
        return YES;
    }
    }
  • 选中页面时触发的方法

     - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
    // 打印选中的索引
    NSLog(@"%ld",self.selectedIndex);
    }
  • 控制more的代理方法

    - (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
    {
    NSLog(@"将要开始编辑more");
    }
    
    - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
    {
    NSLog(@"将要结束编辑more");
    }
    
    - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
    {
    NSLog(@"已经结束编辑more");
    }

block高级

block是匿名函数,能够实现函数的回调功能。用于页面之间通信,同时可以进行传值。
注意:定义属性接收block时,必须使用copy修饰。block释放时,使用Block_Release()

回调传值

举例:在secondViewController中点击右上角的传值按钮,将rootViewController中的导航视图控制器的标题改为“传值”

  1. 在secondViewController.h中声明一个block,并设为属性

    // 无返回值 
    typedef void(^PassValue)(NSString *str);
    
    // 申明属性
    @property (nonatomic, copy) PassValue passValueBlock;
  2. 在secondViewController的点击按钮事件的方法中调用block

    // 点击按钮传值
    - (void)BarButtobItemClick:(UIBarButtonItem *)buttonItem
    {
        self.passValueBlock(@"传值");
    }
  3. 在rootViewController中写实现block的部分(接收传过来的值,一般找哪里有SecondViewController)

    // 实现block 接收传过来的值
    secondVC.passValueBlock = ^void(NSString *str){
        self.navigationItem.title = str;
        [self.navigationController popToRootViewControllerAnimated:YES];
    };
    

运行结果

能实现结果,但是block和ViewController都不能释放。这里存在循环引用的问题。
问题原因:在block块时,引用计数加1
解决方法:加上 __block修饰。因为它有一个引用计数减1的操作

代码修改:

修改实现block的部分的代码,用__block修饰一下self

 __block SecondViewController *mySelf = self;

    // 写block的实现
    touchView.block = ^ void(){
        NSLog(@"我来到了secondVc中");
        [mySelf.navigationController popToRootViewControllerAnimated:YES];

    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { namespace 'com.example.qrtopicture' compileSdk 33 defaultConfig { applicationId "com.example.qrtopicture" minSdk 24 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.3.2' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'com.google.zxing:core:3.4.1' implementation 'com.google.zxing:android-core:3.3.0' implementation 'com.google.zxing:android-integration:3.3.0' implementation 'androidx.appcompat:appcompat:1.4.0' implementation 'androidx.core:core-ktx:1.8.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.5.1' implementation platform('androidx.compose:compose-bom:2022.10.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' }帮我看看
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值