Persistent UIBarButtonItem on all navigation bars within tabbarcontroller - iphone

Ok so I have a tabbar iPhone application. The tabbarcontroller contains about 5 view controllers that are each embedded in their own navigation controller with the exception of 1 in which I just added a navigation bar in IB for a consistent look throughout the app. On every navigation bar in the app, I want a rightBarButtonItem that will open up the same modal view controller no matter where it is selected from. Similar to the "Now Playing" button in the music app, I want the button to stay on every navigation bar regardless of which tab I'm on or how deep I navigate into a navigation controller. How would I go about doing something like this? Is there some way I could simply apply the button to every nav bar in the same way UIAppearance can apply an image to every nav bar in an app? Thanks in advance for any ideas.

A simple approach would be to create a base view controller from which all your other view controllers extend.
With this in place, you could set up a UIBarButtonItem in the viewDidLoad method that attaches itself to the navigation bar and listens for events. From there, it would be quite easy to observe events and present either a modal view controller or push the appropriate view controller on to the navigation stack.
I use this approach for a "logout" bar button item that presents the login view controller when tapped as a quick alternative for users to sign out of their account.
The only down side to this is that each and every view controller that inherits from the base view controller will have the logout button in the navigation bar. Should you need other buttons or want to hide the default button, you need to replace it with another button instead.
It's a quick and easy approach and is also quite simple to remove if required as you can just change the header file to reflect the inheritance rather than sifting through several view controllers removing every instance of the button. It's also easy to maintain as you could overload the button target in any subclasses to perform different functionality when the event is fired.

Related

Navigation between view controllers

On my application, I have four view controllers (VC):
HOME
MESSAGES
PROFIL
SETTINGS
On each VC, an opaque bar bottom with four bar button items for each VC (home, messages, profil, settings).
When I tap on one bar button item, I manage the navigation with a segue (show) and it loads the selected VC.
I would like to find a way to not load again a VC already loaded one time.
For example:
The app is launched
HOME VC is loaded and appears
User taps on PROFIL
PROFIL VC is loaded and appears
User taps on HOME
HOME VC appears without calling viewDidLoad method, because it has already been loaded four steps ago
I tried with all types of segues (show, present modally, etc.) and with presentViewController method, but each time the viewDidLoad method is called.
Is there an easy way to do this or do I have to manage this with a boolean to indicate that the view was already loaded?
I would like a menu like WhatsApp application has for example.
WhatsApp uses a Tab Bar at the bottom. Are you using Toolbar? ViewDidLoad only executes once per view unless you unload the view from memory.
The easiest way to adapt your app is to just add a Tab Bar Controller in your app and then delete the 2 automatically added view controllers and set a relationship (just like a segue; it's below segue types) between each view controller and the tab bar controller. To change the icons, modify them in the Tab Bar Controller View. Then segues will work automagically just like in WhatsApp. :)

Tab Bar mysteriously disappears?

Attached are two images. The first shows my current main.storyboard, and the second shows my problem when I run the app. I have a tab bar controller that has two tabs. On the first tab there is a button. When pressed, the button goes to another view controller with content. At the top is a Navigation bar with a back button. After viewing content, I press the back button, and am back on the original page with the button, but the tab bar is missing. I have seen a few answered questions, but it appears they made their tab bar in the view controller instead of the storyboard. Another says that I should never return to a previous view unless I use an unwind segue. Is this true? If so, how do I set up an unwind segue. If not, how do I fix this problem otherwise? Thank you.
http://i.stack.imgur.com/IYmX2.png
http://i.stack.imgur.com/7slt5.png
The problem is in the wiring of your ViewControllers. You have probably embedded your UITabBarController inside the UINavigationController and not the other way around.
A correct layout looks like this in Interface Builder :
To reproduce:
In Interface Builder drop a UITabBarController. This will come with 2 UIViewController's already wired in.
Pick one of the UIViewController's (let's call it VController1) and click on Editor / Embed in / Navigation Controller. This wires the VController1 to live inside a UINavigationController that is inside the UITabBarController
Add a 3rd UIViewController next to VController1 Let's call it VController3
Wire in a segue between VController1 and VController3, for example with a button.
I hope that's clear enough
Try Linking the button in your viewcontroller (other than the views of the tabbed bar controller) with the tabbed bar controller. Create a segue that links the button with the controller of the tabbed bar application

iPhone App Dev - Loading a view into another view

I have a root view controller with just a simple navigation button that loads a questionview. When I use pushViewController a back button appears. Instead I want a custom button in the top right of the uinavigationcontroller and I want to remove the back button after the page transition.
how can i achieve this..
Take a look at UINavigationItem. You can accomplish both your goals by properly configuring your view controller's navigation item. Use the -setHidesBackButton:animated: to hide the back button, and the -setRightBarButtonItem:animated: to add your custom button on the right side of the navigation bar.

Problem in adding more than 5 button in tab bar controller in iphone

Hello everybody i need your help.
I am making a tab bar based application in iphone. i have more than 5 button to show in tab bar. when i did it, five button displayed fifth button is more..... but when i clicked on more then more button came in a table view not in tab bar.
That's how it works. From the documentation (http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html%23//apple_ref/doc/uid/TP40007457-CH102-SW2):
If you add more than five items to the
viewControllers property, the tab bar
controller automatically inserts a
special view controller (called the
More view controller) to handle the
display of the additional items. The
More view controller provides a custom
interface that lists the additional
view controllers in a table, which can
expand to accommodate any number of
view controllers. The More view
controller cannot be customized or
selected and does not appear in any of
the view controller lists managed by
the tab bar controller. For the most
part, it appears automatically when it
is needed and is separate from your
custom content. You can get a
reference to it though by accessing
the moreNavigationController property
of UITabBarController.
That is intended behavior and not an issue, if you read the viewController Programming guide you could have seen that right away: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457

Is it possible to insert an animation into an UITabBarController thats shown when switching Views?

I have a custom UITabBarController and I want it to slide out the old view and slide in the new view whenever a TabBarItem gets pressed. I looked at UITabBarControllerDelegate but it just offers me to decide if the view gets displayed not how (via shouldSelectViewController).
Is there a way to do that?
(My goal is to have a starfield background that looks almost the same on all the 4 views. It should look like the items get changed and the background just scrolls by a little)
Subclass UITabBarController and make it it's own delegate. Now you can intercept methods like tabBarController:shouldSelectViewController: and manipulate any custom views you have added to the tab bar. And if you add your starfield to the tab bar view, and then use tab controllers that have translucent views the common background can show through.
And you can even intercept tab bar controller methods directly like setSelectedIndex: to have even more control to let you fade things out. But just make sure that you call the super version of the method at some point if you do this so the tab bar remains functional.