How to disable backBarButtonItem displayed in current view? - iphone

After a view has been pushed onto the UINavigationController stack and is displayed I need to disable the Back button under certain circumstances, e.g., when data is being edited on that screen.
The following hides the Back button
self.navigationItem.hidesBackButton=YES;
but I need to disable it.
There are several other answers about how to hide it or suggest not displaying it in the first place, but these are not what I need to implement.

You could implement one of the delegate methods for your navigationController's navigation bar delegate. Take a look at the [UINavigationBarDelegate navigationBar:shouldPopItem:] method.
Returning YES or NO from this method will enable or disable the back button.

You could implement one leftBarButtonItem (invisible and without effect)
add it into your current view (self.navigationItem.leftBarButtonItem)
and remove it to access your backButton.

If you're fine that the whole navigation bar is disabled, an easy solution is
self.navigationController.navigationBar.userInteractionEnabled = NO;
Of course this doesn't work if you have other bar buttons that you want to keep enabled.

Related

In which method should I set my UIToolBar hidden

I have been coding in iphone platform for about a few months. One thing I am still to understand is in which method exactly should I set the navigation bar/toolbar/barButtonItem hidden.
In my project sometimes I set in - (void)viewDidLoad, sometimes I set in - (void)viewWillAppear:(BOOL)animated. Sometimes I set it from where I push the navigation controller.
I think I am not understanding the basics correctly. If one way is not working, I try the other way and somehow it works.
If I am to hide the toolbar or barbuttonitem in navigation controller, where exactly should I set that.
Edit:
If I am pushing a new navigation controller, in which I want my toolbar hidden, where should I set it hidden. Similarly, when I pop it, I want the the toolbar to be shown, where should I set the toolbarHidden property to 'NO'.
Similarly, I have a navigation controller, sometimes it needs to show the toolbar and sometimes it needn't, where should I check the condition for this case.?
You should set this property before the pushing the view Controller and don't need to set No at pop. E.g see the following link:
hiding tabbar on table view cell click

using UINavigationBar without UINavigationController

I want a UINavigationBar at the top of my UIWebView but I want to control by hand the title, the back button, what the back button does - etc. In other words I never want to push or pop views. I just want to change the UINavigationBar contents as the user clicks around the web view.
Where I'm up to is I added the UINavigationBar to my superView and made it 44 pixels tall. How do I set the title since I have no navigationItem? How would I set a fake back button up?
I would appreciate any pointers. I realise this is quite weird what I'm doing.
Thanks :)
The answer, if anyone's interested, is in the class reference of UINavigationBar.
When you use a navigation bar as a standalone object, you are
responsible for providing its contents. Unlike other types of views,
you do not add subviews to a navigation bar directly. Instead, you use
a navigation item (an instance of the UINavigationItem class) to
specify what buttons or custom views you want displayed. A navigation
item has properties for specifying views on the left, right, and
center of the navigation bar and for specifying a custom prompt
string.
In short, use a UINavigationItem and apply it by "pushNavigationItem" on the UINavigationBar.
I created a subclass of UINavigationBar called StaticNavigationBar which I can then load with any state by putting the appropriate UINavigationItem's on it. Thanks for all your answers people.
You can have that UINavigationBar as an outlet and then you can manupulate it as you want. So you don't have to refer to somebody's navigation item. It'll be an independent (sort of) object on your view.
If you dont want the navigationbar as-is youll have to roll your own.
if youre ok with the built in animations for pushing and popping items(dont confuse with push/pop of viewcontrollers) to the bar you would then set your UINavigationBarDelegate and use its methods for controlling how things push/pop, etc.

Where should I "save" changes in my iPhone view to 'create new' of an object?

I have a view that creates a new core data managed object, and fills in all the required properties and also allows optional ones. I originally had a "Done" button on the top left, and when that was pressed, I validated the object then saved and removed the view.
Now I have an edit/done type setup on the top right, so sometimes there are two identical "Done" buttons on the top of the view. I want to switch the left side button so that it just has the normal "Back" button, then somehow validate and stop the view from being removed if it doesn't validate. I can't find any way to capture the method called by that back button and modify it, and viewWillDisappear doesn't work cause there's no way to abort the disappearing.
How can I make this work? I need to validate this, then save, then remove the view if validate and save worked only.
It sounds like your view is a perfect candidate to be pushed modally instead of through the navigation controller stack.
Push the view that creates your NSManagedObject modally:
[self presentModalViewController:yourViewController animated:YES]
Then continue to use your top right EDIT/DONE button for editing/validation as you currently are and when validation is successful simply save your object and dismiss the modal view controller from the parent view controller:
[[self parentViewController] dismissModalViewControllerAnimated:YES];
For more details check http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW14
If you still want to use a button on the left hand side perhaps you can change the right button to say EDIT/CANCEL and add a DONE button on the left side that is only visible when you're not in EDIT mode. If appropriate you can point the DONE button to run through the same validation process before dismissing the modal view using the code above but it probably makes sense that the EDIT/CANCEL button takes care of it.
I hope this helps.
Rog
There is no documented way to intercept the standard back button of UINavigationController. If you want this functionality, your only option would be to customize leftBarButtonItem with a custom button.
When the user taps that button, you can first validate your object and then call popViewControllerAnimated:.
It's hard to mimic the look of the built-in back button, though.

didSelectViewController not being called when switching tabs manually

I have a tab bar interface with three tabs. I would like them to animate when I switch between them. I implemented didSelectViewController (and all the associated delegate stuff) which is called when I press the tabs but not when I switch tabs programmatically. The docs say as much,
"In iOS v3.0 and later, the tab bar controller calls this method regardless of whether the selected view controller changed. In addition, it is called only in response to user taps in the tab bar and is not called when your code changes the tab bar contents programmatically."
Anyone know any workarounds?
Thanks! - Jon
Well, if you are switching them programmatically why can't you create proper animation yourself? I mean you do know which tab gets selected, right?
you can call the method when you switch them programmatically yourself. or write another method to do your animation and call IT whenever you switch tabs programatically

Current UIView Questions iPhone SDK

I posted earlier but am running into similar problems again. Basically the way that my app is setup there is a top bar that is basically just a static image that has UIButtons placed on top of it. That is the Main View Controller and is persistent no matter what view is shown beneath it. I can't use a navigation controller because it is not possible to change the height and I need the bar to be significantly larger than a navbar. However my bar is functioning in much the same way. There is a "Home" Button, a "Back" Button and several destination buttons.
I understand how to switch views from say the home screen. My confusion comes with the back button. In order to press back the app is going to need to know what view is currently being displayed so that it can be removed from view and a new subview can be added. Ideally I would use the UINavigationController so that I can push and pop views which is really what I want to do here, however that is not possible because of the visual problem.
Does anybody know of a method that returns the current displayed view so I could do something like the following
[currentview.view removeFromSuperView];
[self.view insertSubview:experienceViewController.view atIndex:0]
You can use UINavigationController with the nav bar hidden. Put the nav controller inside a view that does have your jumbo toolbar and you'll have access to the push/pop behavior you're looking for.