iPhone - quick Navigation bar question - iphone

I have a UINavigation bar in a few of my views without using a UINavigationController. So i dont use the navigation controller to push new views, I load new views again which have a "static" UINavigationBar at the top.
So currently the navigation bars just show the title of which ever view the user is looking at, they have no other function.
In some of my views I have a requirement to have a back button, about 3 views out of 10.
So I was wondering if it is possible for me to insert a back button that states back and goes back to the previous screen for just these 3 views, so I would have to be able to insert the back button and also detect when it was pressed.
Can I do this with my current set up or do I need to go back and create a view with a UINavigationController and use that to push and pop my views and somehow suppress the back button on the 7 screens that I dont want it to display on?
EDIT:
I've tried the following way:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBackButtonItemStyleBorder target:nil action:selector(myaction)];
[navItem setBackBarButtonItem:backButton]; //Doesn't work
[navItem setLeftBarButtonItem:backButton]; //Works but lacks the back arrow style of the back button
[backButton release];
So I can add a button but it doesn't look like the back button, is there a way to make it look like the back button or should I scratch and just use a UINavigationController? And if I do how can I suppress the back button when I dont want one?

You can if i get you correctly. Just add a custom button to the navigation as leftBarButtonItem or backBarButtonItem and give it a custom button press action. in the button method you can remove this view and show your previous view. But that said the better way is to use normal UINavigationController if your app has Navigation bar in all screens.

Related

Storyboard: Views with navigation controller before tabbar

I have a little problem related to implementing a tabbar with navigationcontrollers, when I have two views before the tabbarcontroller, which also uses a navigationcontroller.
This is my setup in StoryBoard
http://i.stack.imgur.com/8P4Zw.png
http://i.stack.imgur.com/ei3xa.png
But this is not working as I want, because I get two navigationbars (Picture number 2) when I enter the tabbar screen. I know I just can delete the navigation controllers in the tabbar or change the segue to modal, but if I do so, I would not have the ability to add individuel UIBarbuttons to each tabbar view or set individuel navigationbar titles. I would also like to use the push segue through out the app, as it is a kind of an "step by step" app. My question is: How I can eliminate the double navigationbar, when I enter the tabbar, but still have the ability to set a title for each view related to the tab and continue to use the push segue and a navigationcontroller?
I hope you understand my question.
You -can- set individual buttons for each view controller. Grab a tab bar item and drag it to the navigation bar of whichever view controller you want. You can also set the individual title of each view controller. Like you said, there is no reason for the 2nd and 3rd navigation controller you added right after the tab bar controller.
You can set buttons both in the Storyboard and programmatically and they can be different at different stages of your view controller. You should thus delete the second and third navigation controllers and either drag and drop the buttons you want where you need them, and/or deal with it programmatically if necessary.
One example of what you could do programmatically:
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc] initWithTitle:#"yourTitle" style:UIBarButtonItemStylePlain target:self action:#selector(yourSelector)];
self.navigationItem.leftBarButtonItem = buttonSegueBackToAccueil;
This creates a UIBarButtonItem for the controller you are in and places it as the left button of the navigation controller bar. It will run the method "yourSelector" when clicked.

UIBarButtonItem with custom action won't execute it

Well, figuring such thing was easy, I decided to get a custom action executed by my UIBarButtonItem once it's pressed. Here's the code:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"GET BACK!!!" style:UIBarButtonItemStylePlain target:self action:#selector(test)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self.navigationController pushViewController:appsViewController animated:YES];
Yet, even without adding any custom views, -test isn't called at all (although the title change is applied).
As you can see, I'm applying these changes to the item right before I push the new view controller (else the title change wouldn't even work).
I've searched a lot for an answer to this, but the only trouble people seem to be having is it not replying to an action once it has a custom view. So; what am I doing wrong?
You can't set custom actions on back buttons. (Or, rather the action of the back button item is set to a internal one that does the back-behaviour.) If you want to do something when the user comes back to your view controller, consider doing it in viewWillAppear:. You might need to keep track of whether you're appearing after pushing a child view controller or appearing the first time, etc.
The back bar button item just allows you to configure the title to appear in the back button - it isn't stated in the docs but I think that, like a custom view, a custom action is ignored. It just pops the view controller.
Any button that looks like a back button and is in the left of the navigation bar should pop the top view controller from the stack. If you want to do anything else, use the left button item of the top view, or hook into the view will appear / disappear methods or the navigation controller delegate.

Disabling back button and put Cancel button in navigation view controller -- like contacts iPhone application

I have a navigation view controller and there are 3 view controllers in the navigation stack. Now on the third and top most visible view controller I have a default back button coming in.
I need to bring this view controller in edit mode which I did... Now the requirement is to have a cancel button as the left bar button item instead of back button.
This is similar to the functionality given by contacts application of iPhone where you edit a particular contact.
Any clue how to achieve this?
To hide back button and add a left bar button use-
[self.navigationItem setHidesBackButton:TRUE];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector()];
[self.navigationItem setLeftBarButtonItem:leftBarButton];
[leftBarButton release];
And then to programmatically return to the previous view controller, you can do-
[self.navigationController popViewControllerAnimated:YES];
This is a simpler way:
[self.navigationItem setHidesBackButton:YES];
If you are using storyboard, you can also just drag a bar button item onto the navbar where the back button would normally show up. This will override it.

iPhone: capturing back button event

Greetings,
I have the following code for capturing the back button's event:
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStylePlain target:self action:#selector(doLogout:)]];
And here is my doLogout:
-(void) doLogout:(id)sender{
NSLog(#"hi");
}
Everything compiles and runs fine, and the back button text is changed to "Logout".
The only problem is that my doLogout function is never called!!!
What can I do? I've been stuck on this for an hour now... ;(
Many thanks in advance,
The official doc:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
Instead of trying to catch "back button event" why no just just try overriding UIViewController viewDidUnload?
Instead of setting backBarButtonItem on the top view controller, what you could do is set the leftBarButtonItem on the child view controller. This can be any arbitrary bar button item and all action messages should be delivered as normal. Because this takes place of the back button, you have to make sure you manually pop the child controller in one of the action methods.

Can you manually add to an iPhone/iPad Navigation Bar a back button item?

I have a reference to a "UIBarButtonItem", is there a way I can add a custom "Back Navigation Button" to that item when it is not part of a Navigation based view?
I can add a left button:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Custom Back"
style:UIBarButtonItemStylePlain target:self
action:#selector(backAction:)];
menuItem.backBarButtonItem = backButton; //This doesn't seem to work.
menuItem.popOverNavigationItem.leftBarButtonItem = backButton; //This shows a normal button
So how could I make the leftmost button look like a back navigation button?
UPDATE: This other question answered the root of my problem that was leading me to try and do this non-standard UI setup:
iPad: Merge concept of SplitViewController and NavigationController in RootView?
I think the correct way to set the back button is to set it for the view controller that you would be going back to. For example:
RootViewController > DetailViewController
If you want the back button to say "Custom Back" whilst you're on DetailViewController, you have to actually set RootViewController's back button to "Custom Back".
Hope that makes sense.
Having just struggled with this: I think the answer is “no.” You'll need a controller hierarchy.