navigationItem的返回item的title不生效的解决

本文介绍了在iOS开发过程中遇到的问题:使用UINavigationController时无法修改返回按钮的标题。通过重构代码使用自定义navigationBar,并设置navigationItem实现了需求。文章还分享了如何为单独的UINavigationBar添加返回按钮及捕获事件。

这两天在做的一个项目中,有A页面需要push出B页面,刚开始使用了UINavigationController,结果A页面push出B页面的时候,B页面上显示的返回的button的title修改不了,尝试在google和stackOverFlow上找答案,都未果。今天重构代码时,干掉了UINavigationController,使用自定义的navigationBar,再修改navigationBar的navigationItem,达到了修改的方法。

有篇文章介绍得很不错,在此不重写了,直接转载过来。

====================================================================================================

添加类似navigationController自带的返回按钮,效果如下:

左箭头的返回按钮


一、UINavigationcontroller自带的navigationBar 是无法添加左箭头的返回按钮的

在网上搜索了一下 但是真的有方法可以添加成功,但是前提是该 navigationBar不是NavigationController自带的。

如果是自带的navigationBar就无法添加成功,会出现

[csharp] view plaincopy
  1. Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot call setItems:animated: directly on a UINavigationBar managed by a controller.'  

这样的错误,意思就是“ 苹果不允许 直接对受一个Controller控制的naviBar 进行 setItems操作”, 仔细一查,真的有这样的规定

navigationController的属性naviBar

"The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported."

意思很明确了: 这个naviBar是受controller管理的,不支持Pushing, popping or setting navigation items操作。

看到这儿,我也很无语了,所以只好自己切图了,切出左箭头的样式来添加leftBarButtonItem 了。


二、单独的UINavigationBar添加左箭头的返回按钮

但是还是贴出来,单独的一个NavigationBar怎么添加 左箭头的返回按钮代码,这段代码想必看了很多次了吧,哈哈,不厌其烦的在贴一遍好了

[csharp] view plaincopy
  1.     UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@""];  
  2.     [self.naviBar pushNavigationItem:navigationItem animated:NO];  
  3.     self.naviBar.delegate = self;  
  4.  //创建一个右边按钮  
  5.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边"  
  6.                                                                     style:UIBarButtonItemStyleDone  
  7.                                                                    target:self  
  8.                                                                    action:@selector(clickRightButton)];  
  9. [navigationItem setRightBarButtonItem:rightButton];  
  10.     [navigationItem setTitle:@"我的页面"];  
  11.     UINavigationItem *item = [self.naviBar.items objectAtIndex:0];  
  12.     [self.naviBar pushNavigationItem:item animated:YES];  
  13.     UINavigationItem *back = [[UINavigationItem alloc] initWithTitle:@"Back"];  
  14.     NSArray *items = [[NSArray alloc] initWithObjects:back,item,nil];  
  15.     [self.naviBar setItems:items animated:NO];  


如何捕获左箭头返回按钮的事件呢? 你需要去实现 UINavigationBarDelegate 协议里面的方法。

[csharp] view plaincopy
  1. - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item  
  2. {  
  3.     //在此处添加点击back按钮之后的操作代码  
  4.     NSLog(@"aaaaaaaaaaaaa");  
  5.     return FALSE;  
  6. }  

最后贴出运行的效果图

naviBar添加左箭头的返回按钮

[csharp] view plaincopy
  1. <pre name="code" class="csharp"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
更多 0

仔细分析一下,不需要超过15个,就是触发按钮后的某个环节不起作用来。 // // 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 错误在这里,错误发生时,没有执行下面函数,导致removeObject没有发生,因为页面没有变化 TPStrongSelf if ([_self isContainSelectCategoryWithFamliyId:_self.categoryTrafficList[i].familyId]) { [_self.selectCategoryTrafficList removeObject:_self.categoryTrafficList[i]]; } else { [_self.selectCategoryTrafficList addObject:_self.categoryTrafficList[i]]; } [_self reloadView];
最新发布
11-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值