So my app's core is a tab bar. In each of 3 tabs is a UINavigationController subclass. Each one has a different type of table in it, which when a row is tapped, a detail is shown etc.
I currently have a 3 separate subclasses of UINavigationController, one for each tab. Then when a new tab is pressed, the table's controller is pushed.
I just read that you're not supposed to subclass UINavigationController. I'm not overriding any of UINavigation Controller's functionality, but I am overriding it's UIViewController functionality in viewDidLoad. Honestly that's just about it. It seems pretty silly, but Im unclear on how to get the Navigation Controller functionality without subclassing the way I have.
So how am i supposed to have a UINavigationController that I don't subclass? What is the approach that you're supposed to take to switch out the views when a tab is selected?
I'm pretty much a noob. Will Apple reject my app for subcalssing UINavigationController if im only overriding viewdidload?
I've heard folks say not to subclass UINavigationController, and instead 'present it modally.' I have used modal presentation a little bit, but I honestly dont' quite get how it would apply...
Thanks for your help!
What are you doing in viewDidLoad? What about do it in root view controller, not in navigation controller?
P.S. I think Apple will not reject your app for subclassing UINavigationController.
You should use categories to add extra functionality like that. It would look something like this:
#implementation UINavigationController (CustomViewDidLoad)
- (void)viewDidLoad
{
//code goes here
}
#end
You can add this to the bottom of the file that initializes the navigationcontroller
More info about categories (at the bottom): http://cocoadevcentral.com/d/learn_objectivec/
Related
this is probably quite simple.. but I don't know what's the best practice here.
i have a tabbarcontroller with 4 tabs, 3 of them should be a UINavigationController, the other one should also have the UINavigationBar to it but should not be a nav controller itself.
Now i want to give the UINavigationBar a tint color and an action button. I'd like to do this in the interface builder.
The navBar should always be the same one and i only want to create it once.
But where exactly would i do it? Would i create a .xib file with only the UINavigationBar in it and somehow link to that?
I'm very confused here, most tutorials only discuss the navBar for ONE navController but in my case i need the same bar for different ViewControllers..
NavBar comes with the navigationController or can come seperate. It is a subclass of UIView.
You cannot simply share your navBar with all controllers and the non-navigationController. I mean one instance of a navbar. You cannot even set the navbar for a navigationController. It is only readable.
In my understanding you are trying to make the navbar look the same in every controller, am I right?
So you can for instance subclass a UINavigationController and set it up as you want in the init method and init this subclass to add it to the view.
But what about the non-UINavigationController navBar?
Well, that's easy: Simply add your subclassed navigationController to that tab. You don't have to use it as NavController. You simply can add views to his view as to a standard ViewController.
In iOS, why can a UIView (and others) be only embedded in a UINavigationController or UITabBarController? What's special about these two classes?
Edit: oops, wanted to ask 'UIViewController' instead of UIView.
No, its not true. UIView is embedded with UIViewController. This class provides life cycle for UIView. It takes responsibility from initializing the view to deallocating the view.
UINavigationController and UITabBarController are just derived from UIViewController. They provide extra functionality for building hierarchy and switching between hierarchy respectively.
UIView can be embedded in any ViewController or its subclass. UINavigationController and UITabbarController are nothing but subclasses of UIViewController.
I think the embedding you're referring to is the embed in menu item which only allows for UITabBarController and UINavigationController. This means that XCode will take your UIViewController subclass and embed it in one of these two controllers. They are special because they are controllers of other controllers (collections of UIViewControllers). Xcode is simply taking some of the pain out of building a view controller and then adding it to a navigation controller or a tab bar. You can easily embed it in one of these with one click and no code. Much easier than in past versions of XCode.
If you're talking about "embed" as in Interface Builder, yes, as of iOS 5, Interface Builder only gracefully designs user interfaces for three view controller containers, UINavigationController, UITabbarController, and UISplitViewController. These are the three container controllers that come out of the box. You can, though, do your own view controller containment. See Session 102 in WWDC 2011 for information on view controller containment. Also refer to the section on view controller containment in the UIViewController Class Reference.
I am trying to create an app that navigates from the rootViewController using UINavigationBar to the nextViewController which is UITabBarController or similar UIView with UITabBar with couple of UITabBarItems. So please suggest. Suggestions for both programmatic and using Interface Builder would be much appreciated. Thanks
If you read the Apple' docs you'll see that it's clear that Apple think that UITabBarController's should only be used as the root view controller.
I believe the exception to this is perhaps where you have say an interim view controller for login before you enter the enter the app proper.
If you want a tabBarController type behaviour inside a navigationController based app then you should probably be using UIViewControllers with UIToolBars inside UINavigationController and not a UITabBarController.
I think that this post can help you. I'm trying to do a custom tab bar (like foursquare) with Storyboard...
Here's what I have:
A MainWindow.xib file configured with one UIViewController (subclassed to RootViewController). This nib gets loaded at application launch.
RootViewController has two ivars, a custom subclass of UIViewController and a UINavigationController. Both of these are loaded from nibs.
When the application launches both ivars are initialized from their respective nibs, and then the UIViewController.view is added as a subview of RootViewController.view.
Inside UIViewController's view I have a control that triggers an animated swap of UIViewController and UINavigationController. This is where the problem comes in. The swap animates, but the UINavigationController's views are not properly displayed. I get a Navigation Bar with no title, and nothing else.
The UINavigationController nib and underlying functionality have been tested in a stand alone project, but there was no RootViewController.
So, my question is, can I even do this? I've successfully swapped other view controllers like this, but never a UINavigationController. I've seen some documentation that leads me to believe this might be a fools errand, but I haven't convinced myself of that yet.
Solution (Kinda):
I found a solution (workaround? hack?), but it leads to some more questions. I nixed using a Nib for the UINavigationController. Instead, I loaded my UINavigationController's rootViewController from a Nib and then created the UINavigationController programmatically with initWithRootViewController:.
NavRootViewController *navRoot = [[NavRootViewController alloc] initWithNibName:#"NavRootViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:navRoot];
[navRoot release];
This works as I expect. Which leads me to the conclusion that the rootViewController property of the UINavigationController wasn't being set properly when I loaded navigationController from a Nib. And the question is, why? Should it?
Also, when you see something like this happening in one case, but not another, it can be beneficial to either create a subclass and make your nib point at that subclass, or if you already have a subclass use that.
In the subclass, override all the various init:, initWithNibName:bundle:, viewDidLoad:, viewWillAppear:, viewDidAppear: and any other appropriate methods, and in those override, just NSLog("") something about which method it is (with param values perhaps) and call the super implementation.
This will give you an observable "track" of which methods are called in which order, and you can set a breakpoint to see where that call comes from.
This will give you enough information to find missing method calls, and then you can pursue the correct problem either here, or through filing a radar or ...
In some cases, viewDidLoad and viewDidAppear or awakeFromNib may need to be called each time you add the UINavigationController back into the stack of UIViewControllers. It seems that when the typical code executes out of your AppDelegate, that the Window, or something behind the scenes is doing something special for UINavigationController that presentModalViewController doesn't do.
I think you may have missed a conceptual point.
A UINavigationController controls view controllers instead of views. It controls when and where view controllers themselves are loaded. The views themselves are loaded only as a side effect of the pushing and popping of their respective controllers.
Therefore, putting a navigation controller inside of a view controller seldom makes much sense.
If I understand what you are trying to do correctly, you should have the RootController actually set as the rootController property of the UINavigationController (yes the nomenclature is extremely confusing.) Then when your swap event occurs, you should have the navigation controller push the next view. The RootController view will disappear to replaced by the other. then you can repeat the process for an arbitrary number of view controllers.
Only in the case of a tabbar would you want a navigation controller to be a property of a view controller. Even then it should be at the top the tab's hierarchy.
I am using a UINavigationController to handle the pushing and poping of viewControllers in my app. Theres a section where i have a tab bar (not using UITabbarController) which is manageed by the same UINavigationController, i simply add the UITabBar to the navigation controllers view (by using addSubview).
The Problem:
I have some UIViewControllers with table views being pushed into the navigation stack, since my Tab Bar is part of the view and not the navigation stack the TableViews are cut off at the buttom because the Navigation Controller does not know of the tab bar because its in its view and n ot the navigation stack. Without a navigation controller i would just resize the ViewControllers view and it would work fine, but when i try to do that it seems like the NavigationCOntroller just ignores my frame and sets its own and therefore the table views are cut off. I found one solution which was to add some extra cells and hide them and that works sort of OK but its kind of hackerish, anyone have any suggestion of how to go about this in a different non -hackerish way?
Thanks
Alright, so i solved the problem. I had tried resizing the UITableView instead of the viewController before, but this did not work. I just realized though, that this did not work because i was using a UITableViewController which manages its own tableView and was not letting me change the frame of it (maybe i was changing it in the wrong place, tried in viewDidLoad, i bet if i did it after the call to [super viewDidload] it would have worked..o well). So I changed the class to a UIViewController and managed the table view in there, now it works good, thanks for the replies.
Try making the root view a UIView with a UITableView for a subview. Then add the UITabBar to the UIView instance. Now the UITableView won't know about the UINavigationController.