Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 7    Views: 201

PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
03/18/14 05:58 PM (10 years ago)

Customising Back button text

I've been playing with buttons in the nav bar, swapping labels for icons etc, basically seeing what's possible. Usually, navigating from one screen to the next and then back to it again would show the title of the first screen as the text for the 'Back' button on the second screen. For various reasons, that wasn't ideal in my project. This snippet allows you to change the text of the Back button on the second screen without needing to completely redefine the back button on the second screen. I found that if you do redefine it for the second screen, you lose the native 'Back' behaviour. This keeps it. It needs to be placed in the view controller of the FIRST screen to get it to work. I put it in just before the app delegate methods down near the bottom of the file. Now, regardless of what the first screen is titled, the next screen's Back button won't use this screen's nav bar title as its label. Instead it will use whatever you put in the 'initWithTitle' definition below. - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // self.title = @"Leave this blank to show the default title for the second screen, or change it if you need to. (Remember, you've already given the second screen a title in the BT Control Panel...)"; self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Whatever you want your Back button to say on the second screen...but please make it shorter than this!" style:UIBarButtonItemStylePlain target:nil action:nil]; } Found it here: <a href="http://stackoverflow.com/questions/20282760/back-text-displayed-in-ios7-navigationbar-when-view-title-is-long" target="_blank" rel="nofollow">http://stackoverflow.com/questions/20282760/back-text-displayed-in-ios7-navigationbar-when-view-title-is-long</a> Works like a charm for me, I hope others might find it useful. Cheers Paddy.
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
03/19/14 06:26 AM (10 years ago)
Thank you, excellent tip!
 
peterj
Apple Fan
Profile
Posts: 113
Reg: Jun 19, 2011
location unknow...
6,630
like
03/22/14 01:07 PM (10 years ago)
Worked like a charm - thank you!
 
Tyroner
Aspiring developer
Profile
Posts: 175
Reg: Dec 26, 2012
Johannesburg, S...
4,200
like
04/11/14 04:49 AM (10 years ago)
I'm a bit lost here as I'm a beginner with Objective-C! "It needs to be placed in the view controller of the FIRST screen to get it to work. I put it in just before the app delegate methods down near the bottom of the file." Im not sure where exactly in Xcode it must go.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/21/14 07:09 PM (10 years ago)
Tyroner it's a bit of a mind-bender, I know! Basically you have a view controller that (unsurprisingly!) controls the view for a given screen, which in BT lingo = plugin. So if you're navigating from one screen to another, you're either moving (a) from one plugin to another, or possibly (b) from one instance of a plugin to another instance of it. What I've given above will only work in (a), I think. Find the plugin that you're starting this navigation behaviour from (eg BT_screen_map or whatever it is in your case). That's what I'm calling your 'first screen'. You'll find it in your XCode list on the LHS inside the BT_Plugins 'folder'. Locate your screen/plugin in there. Now find the view controller for it - it will be called bt_PluginName_viewController or similar. That's the one that you need to add the code to - as I said, down the bottom of that file. Good luck! Cheers Paddy.
 
Jen
Lost but trying
Profile
Posts: 70
Reg: Mar 14, 2013
Melbourne
2,750
like
04/29/14 11:30 PM (9 years ago)
Hi PaddyO. The explanation above is awesome. Just one query, if I may take a moment of your time. I don't have a view controller in my relevant plugin file. Just: Cr_menu_advanced.h Cr_menu_advanced.m cr_menuadvanced_cell.h cr_menuadvanced_cell.m There is a BT_viewController in the Layout folder. Both .h and .m Do you know where to enter your handy code? Thanks heaps from Sunshine West :) Jen
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/30/14 12:38 AM (9 years ago)
Hi Jen, from what I've been able to discover, this only works when you're moving from one screen (plugin) to another and you want to go back again. I'm assuming that your cr_menuadvanced.m is the screen that you're going FROM, so you want this changed behaviour on the next screen, yes? I'm taking a guess that cr_menuadvanced.m is the view controller here, only b/c cr_menuadvanced_cell.m sounds more specific, ie its about the view of individual cells rather than the whole screen, if that makes sense (cells won't have their own nav bar but the full screen will). OK, assuming that's true, look in cr_menuadvanced.m, I reckon that's the view controller you want. And you're going FROM it elsewhere and back again, so this is the controller to add the code to. If it has a viewDidAppear method, add the code below in as the last bit of code in that method. If it doesn't (it might just have a viewWillAppear method or similar), create a viewDidAppear method after any other view methods and add the code in to that. I dug through the plugin that I used it on, here is what I have.. these are comments to myself so use or ignore what suits you: /** This changes the text for the "Back" button on the next screen without needing to re-code the Back button there. So now regardless of what this screen is called, the next screen's Back button won't use this screen's nav title as its label. Instead it will use whatever you put in the initWithTitle definition. /*This isn't relevant except it shows where to insert the code in my case: * Notifies the view controller that its view was added to a view hierarchy. * * @param animated If YES, the view was added to the window using an animation. */ //This is the good stuff: - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //This was what I originally had: // self.title = @"Leave this completely blank for the default screen title (ie remove these words) or change it if you need to"; // self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list.png"] style:(UIBarButtonItemStylePlain) target:nil action:nil]; // But I like this better... //This simply replaces the "Back" chevron (<) with an icon of your choosing. You need to then remove the label from the button //self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"pin.png"]; //or leave that alone to retain the chevron //self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"blank.png"]; //This overrides the default label and gives an empty label "" instead self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; } Note that you don't have to actually add 'blank.png' to your project; either it already exists in your project in a framework somewhere, or Xcode is clever enough to know that you mean 'no image' but it is sometimes required to act as a dummy image anyway, showing nothing. Which one I'm not sure, I haven't looked for an actual blank.png image yet! See if that does it for you. Cheers Paddy
 
Jen
Lost but trying
Profile
Posts: 70
Reg: Mar 14, 2013
Melbourne
2,750
like
04/30/14 12:41 AM (9 years ago)
basically all i want to do is take away the word 'back' and leave the arrow. I very much appreciate your help! i'll have a look at it tomorrow and let you know. Jen
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.