Reputation: 1970
When a user segues to a second VC from within a starting VC that is embedded in a UITabBarController
, I change the title of the UITabbarItem
with some code placed in the viewWillAppear
method of the second view controller.
//Second VC, View WIll Appear
UITabBarItem *selectedItem = self.tabBarController.tabBar.selectedItem;
if (selectedItem) {
selectedItem.title = @"VoiceMail";
}
This works fine.
When the user returns to the starting view controller, I want to switch the title back.
I tried to do this by placing similar code in the view will appear method of the starting view controller.
Starting VC: ViewWIllAppear
UITabBarItem *selectedItem = self.tabBarController.tabBar.selectedItem;
if (selectedItem) {
selectedItem.title = @"Phone";
}
But it is having no effect, leaving the title as Voicemail
.
WOuld appreciate any suggestions on how to change back to initial value.
Thanks for any suggestions.
Upvotes: 0
Views: 42
Reputation: 77690
Trying to change the "2nd" tab title by referencing .selectedItem
in your "Starting VC" won't work, because at that point .selectedItem
is StartingVC
.
One approach would be to save a reference to the index of SecondVC
... then, inside that VC, on viewWillDisappear
you can reset its tab's title:
This is all in SecondVC
:
#import "ChangeSecondViewController.h"
@interface ChangeSecondViewController ()
@property (assign, readwrite) NSInteger myTabIndex;
@end
@implementation ChangeSecondViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UITabBarItem *selectedItem = self.tabBarController.tabBar.selectedItem;
if (selectedItem) {
selectedItem.title = @"VoiceMail";
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
_myTabIndex = self.tabBarController.selectedIndex;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
UITabBarItem *myTabItem = [[self.tabBarController.tabBar items] objectAtIndex:_myTabIndex];
if (myTabItem) {
myTabItem.title = @"Phone";
}
}
@end
Upvotes: 1