iPhone: capturing back button event - iphone

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.

Related

stop view from disappearing

I am launching a viewController from another view controller via the push on its table views cell. Now on the second view controller I have a whole bunch of controls mainly test fields. I would like to by using the default back button provided in the second view controller so it'll be the title of the first view controller and keep in mind like I said default, so I don't want to create my own button for back on the second view controller. So would like to detect if the second view controller is exiting or disappearing or will disappear and based on certain conditions stop it from going back to the original caller view controller. I originally assumed it could be done in here:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
{
// So back button was pressed on the second view controller, so how do I stop it
// here from going back to the original view controller.
}
}
Or how do I do it? I can't seem to find a view controller return type BOOL method to call and stop it.
Thanks.
Note that method is called viewWillDisappear, not viewShouldDisappear, so Apple won't let you stop the view from disappearing at that point. And from the user's point of view, pressing the back button should indeed take them back. Instead you might consider hiding the back button under the circumstances where it's not allowed.
You can't prevent the view controller from closing however you can emulate it.
This line will replace the close button with a button that will call a homemade function
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(closeBtn)];
So you will be able to close the view if you want:
- (void) closeBtn
{
bool shouldClose=true;
// Your conditions here
if (shouldClose) [self dismissViewControllerAnimated:YES completion:nil];
}
sorry,i'm not good at english,so reading so long text is a little difficult for me.i can only get that you want to go back without a button. i known navigationController has a method to go back to previous view.
Make a custom back button and use self.navigationItem.hidesBackButton=YES
Add target to your custom back button and use it as you like. When you want to move the view you can use [self.navigationController popViewcontroller].

Hide BackButton in UINavigation bar

i am pushing a View om RootView controller ,i get the navigation bar with back button which is navigate to rootviewcontoller,but i don't need this back button.i pout this code in the viewcontoller to hide the back button
self.navigationItem.hidesBackButton = YES;
so i get the hidden back button,but here is the problem,i need leftnavigationbar button to show something,when i write the above code ,the letbarbutton also hidden from the view.becz the leftbarbutton act as the back button.
My need is to block the letbarbutton to become a back button when push from the rootviewcontroller,i didn't need a navigation back to rootviewcontoller.But defenitly need leftbarbutton there for my need in the view.
How can it possible to do?.Please help me to do this.
Thanks in advance.
try this.
self.navigationItem.backBarButtonItem = nil;
it works for me
Why don't you just set the letbarbutton and add no action to it?

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.

iPhone - quick Navigation bar question

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.

How to display back button in nav app?

I'm pushing a tableview in a navigation based app. The pushing view (viewOld) and pushed view (viewNew) are both UITableViewControllers. I have given viewNew a title from viewOld. Once viewNew appears, I see the title but no back button on the left. Shouldn't a back button appear once you give the view (viewNew) its title?
I can click the empty space on the left of the navigation bar in viewNew and I go back to viewOld. But why is the back button not visible? I am doing this in OS 3.0 but I don't think the functionality or behavior of the back button has changed from previous versions.
Pushing viewNew from viewOld:
ViewNew * viewNew = [[ViewNew alloc] initWithNibName:#"ViewNew" bundle:nil];
viewNew = #"The new view";
[self.navigationController viewNew animated:YES];
[viewNew release];
The back button is the title of viewOld, unless viewOld's navigationItem has a backBarButtonItem set, which overrides it. If viewOld doesn't have a title and doesn't have a backBarButtonItem set, then the back button won't appear.
Note that if you want to set the backBarButtonItem, its target and action should both be nil.
I had the same problem: the Back button text was hidden but it worked when pressed on the left.
So I found this:
I just saw this the other day. What I found interesting was that touching in the area of where the back button should be actually worked.
I finally decided it had to do with the text of the back button. It pulls the text from the title of the parent view controller. If there is no title, there is no text for the button, and it seems Apple made it not display a button if there is no text. So, either specify a title on the parent controller, or if you don't want that, I believe you can specify the text that the back button will display (this is specified in the parent view controller, not the child).
That was all!