iOS: Cannot get view to display with Navigation Controller - iphone

I'm trying to display a controller loaded from a NIB in my navigation controller (iOS 4/Xcode 4), but it is not working. Interface Builder doesn't allow me to choose any of my nibs; when I try to manually type one I get this error:
warning: Unsupported Configuration: Navigation Controller NIB Name set to MyViewController.nib (This view controller is not intended to have its view set in this manner)
What's this all about? One thing to make note of: I manually added the Navigation Controller after creating a view-based project. I decided that I should use one after I had already created the project, instead of choosing Nav-based from the beginning. Perhaps I forgot a setting?

I think you're trying to set the NIB of the navigation controller itself.
This is not what you want to do: you instead want to set the nib of the root view controller of your navigation controller. To do this, you should expand the navigation controller via the navigation panel (the left hand panel with a list of all the objects in your current NIB).
Selected 'Root View Controller', and then set its NIB and class as appropriate.
The reason you're getting an error right now is you're actually trying to set the navigation controller's NIB, which XCode is quite rightly not letting you do.

Related

Present ViewController from its title

Background
I am developing an application which contains a top nav bar with an icon that allows to go back to the main ViewController.
As there as several ViewControllers, I don't want to duplicate the segue on each ViewController. I would prefer to create a custom class for the top nav bar and be able to programmatically present the main ViewController. I found that I could maybe call present(viewControllerToPresent:animated:completion)
Questions
Is this the right way to do it?
How do I access the controller I need to pass to this function?
Is there a method which allows to get the controller from its title?
Also, as the main ViewController has already been instantiated when the application started, do I need to instantiate a new one or can I get a reference to the existing one?
So if I get this correct you click mainVC->firstVC->secondVC->nthVC and then you'd want to go directly back to mainVC ? View controllers are stacked so what I'd do is just dismiss all those view controllers that are above mainVC.

viewDidLoad nor init are being called at application start iphone

This is a novice error for sure. In Xcode 4 i have created a Window Based application in which i put a TabBarController as the root controller. When the application starts, the first tab of the tabbar is selected and thus the view corresponding to that tab is shown. The problem is that the View Controller that corresponds to that view in the first tab is not being read, i put some logs in the init, viewWillLoad, and viewDidLoad methods and none of them are being shown. I have linked the controllers correctly, plus Xcode links them automatic if you select that option when creating a new view controller. Here's how it looks like in my project.
This is how it looks like in the tab controller i have linked to the view.nib:
And this is the view, it is linked to the view controller:
And here's the View Connected to the File's Owner Outlet:
Everything looks fine for me so i don't understand why it's not calling those methods in the view controller when the application starts or when the tab corresponding to that view is being selected.
Thanks in advance for the help.
I fixed it. The problem was that the referencing class was UIViewController intead of the view controller i created for it. The answer was to go to MainWindow.xib then select View Controller - Home then go to Identity Inspector and select the View Controller i created for it in this case the HomeViewController.h

Iphone using mainwindow.xib confusion

In a navigation based application, when I want to create and use other uiviews and uitableviews I need to create their controller and views. in an example I saw that I can simply create a new controller with .xib file, design it, and just call that xib file from my navigationcontroller.
In another example, some stuff was going on also in the mainwindow.xib and some new controllers and navigation items were added from the mainwindow.xib.
What is the difference between these methods? when and why I should need to open and edit the mainwindow.xib file to add a controller?
The mainwindow.xib is your UIWindow component which you can see as a representation of your iphone screen, it will always be there no matter what. In your examples when you are showing your view controller dirrctly that is because the controller is already a subview of your UIWindow which is the mainwindow.xib in the Interface Builder.
There really is no difference between the 2 methods, in the first one you are adding your controller as a subview progrmatically using:
[window addsubview:mynavcontroller]
And in the second one youbare doing it thru interface builder, you may use whichever method you feel more comfortable with.
You do not really need a controller to show a view, however they can be handy if you want to do any extra stuff such as rotating your view or loading certain data when the view is loading. That being said you could add your view as a subview of your window and it would still work.

What are the conventions for declaring a UITabBarController in mainwindow.xib?

I have a mainwindow.xib file with a UITabBarController as the base view controller of the app. So inside the UITabBarController I've added about 10 sub UIViewController objects as tabs. Most of them are just a UITableViewController subclass or a UINavigationController containing a UITableViewController subclass.
In this design, each UIViewController is fully loaded on app startup, including calling the viewDidLoad method of each view controller. Is there any way to get around that? Since the view controllers are just UITableViewControllers with no other outlets, it seems excessive to create a NIB for each tab (which I assume would allow the viewDidLoad to only get called when the user first switches to the tab? Or am I wrong on that?)
Anyway, my question mainly, is: how is it conventionally done? If you have 10 different view controllers on one UITabBarController, do you put them all in mainwindow.xib? If so, should each have its own NIB, and if not, where do you put them, and how do you add them to the tab bar?
What you want to do is to define the UIViewController views in a different xib file for each view - the reason they all get instantiated is that when the xib loads, all objects held in the xib load - and that means all your views and view controllers since you have defined them there.
In MainWindow.xib where you have the tab bar defined, you can still set within each tab the view controller type that will be called and also the XIB file to use for that type (create a new project with the "TabBar application" template and the second default view will be like this).
Then as you press tabs the view controllers will be instantiated from the different XIB files you have defined.
Note that this means if you are using IB to add buttons to the navigation bar, you have to do that back in the TabBar xib and not in the xib you use to define the view. You can still link actions to the view controller definition within the tab.
The way Apple suggests doing it for pure NIB files is how you say: Each sub-view in its own NIB file.
Instead of doing this, I would create the UITabBarController programmatically. That way you can define all your simple views in code, and still load complex views from NIB files.
Personally, I prefer creating as many of my views programatically as possible. The compiled code has a smaller footprint than the NIB files and I feel like I have more control. I mostly use Interface Builder to mock up applications.

Managed Object Context in Tab Bar View

Ok. this one's a challenge.
I have a tableview within a navigation controller.
I push it from the root, where I have an add action that allows me to add a new record. That works fine.
Now what I've tried to do is add this tableview to a tab bar view (without a tab bar controller cuz that won't work) but within the same navigation controller.
So what I want to do is this: Root > TabBarView (loads Tableview) > add new record.
The problem lies in the managed object context, I get the whole "can't find entity error" but I have no idea how to fix it.
I've managed to get the AddRecord modal view controller to show up from the tabBarView, but it presents itself without a navigationbar, whereas if I try to add a record in the solitary tableView (outside of the tabbar) its no problem.
I'm now calling my methods from the TabBarView's navigationBarbuttons, routing through to the tableviews methods.
I know my methods have to be called from the tabBarView instead of the actual tableview now, and they do fire, but I don't know how to manage the MOC when its in a tabView.
Oh, and this is based on coredata recipes and books, so when the add record method is fired, it creates a new MOC to create it, then reintegrates back in the main MOC when you're done.
Any ideas?
It sounds like you have a couple of problems.
"Can't find entity" error -- this depends on which Managed Object Context you're using. If you created a separate MOC to manage the object you're editing (which is a good idea, by the way), make sure you assign it a Persistent Store Coordinator. This is how an MOC discovers what objects are available. If you're using the MOC created in the App Delegate, make sure you're spelling the name of the entity correctly.
No Navigation Bar in sheet -- When you push a view controller onto a navigation controller, its navigationItem is used to populate the navigation bar. When you present a view controller as a sheet, only the view controller is displayed. It is not embedded in a navigation controller. In order to get the navigation item to display, you'll need to create a new navigation controller with your view controller as the root, and then present the navigation controller's view.
As far as the main MOC goes, views and controllers should be irrelevant. Obtain a reference to the MOC in whatever controller you are using and operate with that MOC. If your application delegate creates the main MOC, make it a property of that delegate and access that from your view or tab controller.
I don't quite follow what navigation problem you're having, but if there's no navigation bar when you need one, I suspect you need to create and add a UINavigationController somewhere that you're adding a UIViewController subclass. Make the subclass the root of the new UINavigationController and put the controller into the tab or whatever.
Your managed object context (MOC) should not be dependent on navigation or views. It's part of the model. (Although as you know, a 2nd MOC for a cancelable edit view would be dependent on navigation to the extent that you create it for use by the editor.)