viewWillAppear:等无法调用的总结

本文探讨了在iOS开发中使用UINavigationController时遇到的问题,即将其view作为subview添加到其他viewController的view中,或将其添加到UITabbarController中,导致NavigationController的stack中的viewController无法接收特定方法的调用。文章提供了两种解决方案来解决这一问题。

今天也遇到了这样的问题。

产生原因是用了UINavigationController. 
将UINavigationController的view作为subview添加到了其他viewController的view中。
或者把UINavigationController添加到UITabbarController中了。

此时,NavigationController的stack里面的viewController就收不到-(void)viewWillAppear:(BOOL)animated;等4个方法的调用。

原因还不敢确定,应该是这样的结构破坏了消息的响应链。导航控制器上层的viewController只是接受了导航控制器的view,而不是controller. 估计tabbarController内部也是处理了set进去的controller的view。

解决方法两种:
1,在导航控制器上层controller的viewWillAppear中显式调用viewWillAppear方法。
 

复制代码
  1. -(void)viewWillAppear:(BOOL)animated 

        [super viewWillAppear:animated]; 
        [selectedViewController viewWillAppear:animated];  //tabbarController直接用selectedViewController更方便
    }

 

2,把导航控制器上层controller设为UINavigationController的delegate,在

 

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated ;中显示调用viewWillAppear。

 

复制代码
  1. -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
    {
        [viewController viewWillAppear:animated];
    }
我点击了已被选中的第一项希望执行取消逻辑,通过断点可知失败的直接原因是[_self.selectCategoryTrafficList removeObject:_self.categoryTrafficList[i]];虽然执行了,但是没有成功删除_self.categoryTrafficList[0] // // SDNV6SiteInsightCategoryDetailVC.m // Omada // // Created by wangchen on 2025/10/9. // Copyright © 2025 TP-Link. All rights reserved. // #import "SDNV6SiteInsightCategoryDetailVC.h" #import "SDNStaticsCategoriesDetailCell.h" #import "SDNCommonStaticCategoriesTool.h" #import "SDNInsightAppCategoriesDetailView.h" #import "TPConvTools.h" @interface SDNV6SiteInsightCategoryDetailVC () @property (nonatomic, strong) NSMutableArray <DMSDNCategoryTraffics *> *selectCategoryTrafficList; @property (nonatomic, strong) NSMutableArray <DMSDNCategoryTraffics *> *categoryTrafficList; @end @implementation SDNV6SiteInsightCategoryDetailVC - (void)viewDidLoad { [super viewDidLoad]; } - (void)tpbSetupInitialData { [super tpbSetupInitialData]; self.navigationItem.title = gMeshQuickSetup.sdnStaticsTrafficCategories; self.selectCategoryTrafficList = self.selectCategoriesInfo.categoryTraffics.mutableCopy; self.categoryTrafficList = self.categoriesInfo.categoryTraffics.mutableCopy; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self reloadView]; } - (void)reloadView { [self updateSelectCategoryTraffic]; self.sectionArray = @[[self categoriesPieChartSection],[self categoriesListSection]]; } - (TPBTableSectionModel *)categoriesListSection { TPBTableSectionModel *section = [[TPBTableSectionModel alloc] init]; NSMutableArray *cellModelArr = [[NSMutableArray alloc] init]; for (int i = 0; i < self.categoriesInfo.categoryTraffics.count; i++) { UIColor *pieColor = i < [self colorArray].count ? [self colorArray][i] : [UIColor tpbPieOther]; SDNStaticsCategoriesDetailCellModel *cellModel = [[SDNStaticsCategoriesDetailCellModel alloc] initCellModel]; cellModel.categoryTraffics = self.categoriesInfo.categoryTraffics[i]; cellModel.tintColor = pieColor; BOOL isSelected = [self isContainSelectCategoryWithFamliyId:self.categoryTrafficList[i].familyId]; TPWeakSelf cellModel.clickSelectInfo = ^{ TPStrongSelf if ([_self isContainSelectCategoryWithFamliyId:_self.categoryTrafficList[i].familyId]) { [_self.selectCategoryTrafficList removeObject:_self.categoryTrafficList[i]]; } else { [_self.selectCategoryTrafficList addObject:_self.categoryTrafficList[i]]; } [_self reloadView]; }; cellModel.isSelect = isSelected; [cellModelArr addObject:cellModel]; } section.cellModelArray = cellModelArr.copy; NSString *actionTitle = @""; if (self.selectCategoryTrafficList.count != self.categoryTrafficList.count) { actionTitle = gControllerQuickSetup.controllerQuickSetupSelectAll; } else { actionTitle = gSDNGlobal.selectNone; } TPWeakSelf TPBTableSectionViewAction *headerAction = [TPBTableSectionViewAction actionWithTitle:actionTitle actionCallback:^{ TPStrongSelf [_self clickSelectOrUnselectAll]; }]; headerAction.actionTitleColor = [UIColor tpbGreen]; section.headerTitle = gMeshQuickSetup.sdnStaticsCategories; section.headerAction = headerAction; return section; } - (void)updateSelectCategoryTraffic { self.selectCategoriesInfo.categoryTraffics = self.selectCategoryTrafficList.copy; long totalTraffic = 0; for (int i = 0; i < self.categoriesInfo.categoryTraffics.count; i++) { if ([self isContainSelectCategoryWithFamliyId:self.categoryTrafficList[i].familyId]) { totalTraffic += self.categoryTrafficList[i].traffic; } } self.selectCategoriesInfo.totalTraffic = totalTraffic; } - (TPBTableSectionModel *)categoriesPieChartSection { TPBTableSectionModel *section = [[TPBTableSectionModel alloc] init]; SDNInsightAppCategoriesDetailView *customView = [[SDNInsightAppCategoriesDetailView alloc] init]; customView.selectCategoriesInfo = self.selectCategoriesInfo; [customView updateViewWithCategoriesInfo:self.categoriesInfo]; TPBCustomViewTableCellModel *cellModel = [TPBCustomViewTableCellModel customContentWithView:customView action:nil]; section.cellModelArray = @[cellModel]; return section; } - (void)clickSelectOrUnselectAll { if (self.selectCategoryTrafficList.count != self.categoryTrafficList.count) { self.selectCategoryTrafficList = [self.categoryTrafficList mutableCopy]; } else { [self.selectCategoryTrafficList removeAllObjects]; } [self reloadView]; } - (BOOL)isContainSelectCategoryWithFamliyId:(long)famliyid { BOOL isContain = NO; for (DMSDNCategoryTraffics *category in self.selectCategoriesInfo.categoryTraffics) { if (category.familyId == famliyid) { isContain = YES; break; } } return isContain; } - (NSArray<UIColor *> *)colorArray { NSMutableArray *colorMuArray = [[NSMutableArray alloc] init]; [colorMuArray addObject:[UIColor tpbPie1]]; [colorMuArray addObject:[UIColor tpbPie2]]; [colorMuArray addObject:[UIColor tpbPie3]]; [colorMuArray addObject:[UIColor tpbPie4]]; [colorMuArray addObject:[UIColor tpbPie5]]; [colorMuArray addObject:[UIColor tpbPie6]]; [colorMuArray addObject:[UIColor tpbPie7]]; [colorMuArray addObject:[UIColor tpbPie8]]; [colorMuArray addObject:[UIColor tpbPie9]]; [colorMuArray addObject:[UIColor tpbPie10]]; [colorMuArray addObject:[UIColor tpbPie11]]; [colorMuArray addObject:[UIColor tpbPie12]]; [colorMuArray addObject:[UIColor tpbPie13]]; [colorMuArray addObject:[UIColor tpbPie14]]; [colorMuArray addObject:[UIColor tpbPie15]]; return [colorMuArray copy]; } @end
12-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值