App crashes when tab bar item is selected - iphone

I have a tab bar item with a UIScrollView and all the code works.
I link the UIScrollView IBOutlet in IB to a UIScrollView I placed in my View.
When I run the app and select the tab bar item I get the following error.
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key scrollView.'
I have no idea what this is. When I disconnect the ScrollView IBOutlet, then there is no crash.
Thanks

You probably renamed the name of the scrollView in the code without re-connecting the outlet in Interface Builder. Double check your code to make sure the spelling is the same as in Interface Builder, and for good measure, reconnect the outlet to scrollView.
Referring to this Apple forum thread, UITabBarController has multiple UIViewControllers each of which has its own nib file assigned to it. It's up to you to make sure you change the UIViewController's class file: in the nib file for each view controller and in the tab bar controller's nib file, you also have to do the same thing by changing each view controller's nib file and class.
So all in all you have to update settings in two places: the individual nib files for the view controllers and the tab bar controller's nib file. Think of it as creating the connections/bridges between the individual nib files and the tab bar controller.
Once these connections are made, your program should have no problem accessing your scrollView outlet, since that is how it ultimately accesses it: by using key-value coding by means of your tab bar controller (at least, that's my understanding). As of now it can't because the tab bar controller is not connected properly.

Related

Tabbar controller with navigation controller in a single application

I have created an empty application and then added a window to it(made a window based application).
Then i added tabbarcontroller to it and navigation controller inside the tabbarcontroller. I have added nib files to my application for each tabs and given the nib names to viewcontrollers.Then i added a button to one of the nib file ,then application is crashing and giving error like -
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[ setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key btnNext.'
-----i have connected the iboutlet of buttons too.then why this error?
This error happens when the IBOutlets are not wired correctly. One common example is if you were to wire an IBOutlet in Interface Builder, but then delete the corresponding property in the corresponding class (where the IBOutlet was defined). Obviousily that's a mistake when that happens, but unfortunately Xcode will not give you a compile time error, and instead gives you this vague runtime error. Take a look at ALL IBOutlets in the relevant Interface Builder files and make sure they match up correctly to the IBOutlets in the header files (both in type and presence).
What you need is to add navigation controller to tab and inside the navigation controller in (view controller) you need to put the class name of view controller. Hope this link will also help you.

how to link view controller and .xib file to tab bar item in xcode 4

I have a tab bar with 4 items and i want to link the .xib files to each item. The problem is that the NIB File Name shows no items, and if i write the .xib for example, StartView without the .xib i get an error. I get the same error when i link the tab item to the view controller class in the Identity Inspector. This is the error:
'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "StartView" nib but the view outlet was not set.'
I had no problem with this in Xcode 3. As far as i know i don't need outlets in my view controller to be able to link a view to a bar item.
Any ideas on how to fix this?
Thanks.
Each .xib has a main view that must be connected to its files owner. Usually when you change the class name of a .xibs owner, the view outlet will become disconnected. So you'll want to first check that the class name of your .xib is the custom controller you have made. You then have to open the .xib and click on the connections inspector. Then just drag and connect the view outlet (labelled as view in the connections inspector) to your view.
Cheers.
Problem solved, but i didn't like the solution. I created the view controller again and selected to create a .xib file along with the controller and that was it. Then i made the usual references from interface builder and it's working. I didn't like it because the .xib file has the same name as the view controller class. If anyone knows how to create the .xib file and the view controllers separately and get it to work, please inform me, i would really appreciate it.
select create new file, objective c, check the make xib file box and in the name of it, add .xib extension to the end of the name

Problem Loading Initial Nib in iPhone Program

I'm having a problem similar to the one discussed in this thread, but the solution provided to him isn't working for me. I apologize for the lengthy description, but this is my first post on SO and I want to be complete.
I have a program that runs fine in the simulator. However, when I try to push it out to the device, I get an error that says this:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/....app> (loaded)' with name 'MainWindow''
So: it's trying to load a XIB called MainWindow, but it can't find it. That's because I don't have a XIB called MainWindow; the first XIB I load is called FrontPage.xib. I didn't start this program using one of the templates and thus was not provided with a MainWindow.xib file at the beginning.
The Google machine tells me that the Main nib file base name can be modified in my app's plist. So, I go there and I change it to FrontPage. Now, I get a different error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x12c270> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
I'm pulling my hair out over here - anyone know a solution?
Make sure that your view is connected to your "View" outlet in Interface Builder. You do this by right clicking on your "File's Owner" in IB and then dragging to the "View" outlet to your View. Here's an illustration from a game I'm working on. Notice the blue line, dragged from the dot on the right to the "view".
Another thing you will want to check is your Info.plist. Make sure that you have called your nib file FrontPage and not *FrontPage****.nib***.
If Frontpage.xib is going to be your application's main xib, then you have to create a Window object and an App Delegate object in that xib in order for the app to be able to load. Before you can start presenting view controllers and such you have to configure the app and the window or it will crash as you describe. I would suggest creating a new project using a Window based template and just take a look at the MainWindow.xib that gets created automatically and try to mimic it in your Frontpage.xib..
.. Although I would just sincerely suggest using a MainWindow.sib in your project and making your Frontpage.xib a normal view controller xib. If you choose to add a new file to your project, then choose User Interface, then choose Application xib instead of View xib, then it will set you up with something close to what you need.
The project templates are important and useful. You should always use them. I start all of mine with a Window Based template. It is the most basic template and the most flexible.
If the view controller for FrontPage.xib is set properly by setting the File owner property via IB then you also need to set the view property. you can do this by control dragging from Fileowner to the view. From the error it appears that you have yet not set the view property for your view. Open the nib in Interface builder and and look for the View Connection in connection Inspector , this must be connected to file owner's view outlet. You don't need to declare it as a property anywhere.

issue with tab bar view displaying a compound view

I created a tab bar application, and I make the first tab a table. So I create a tableView controller, and go about setting the class identity of the view controller for the first tab to my tableView controller.
This works fine, and I see the contents of the table filling up the whole screen.
However, this is not what I actually want in the end goal - I would like a compound window having multiple views:
- the aforementioned table
- a custom view with data in it
So what I do is create a nib for this content (call it contentNib), change the tab's class from the tableView controller to a generic UIViewController, and set the nib of that tab to this new contentNib.
In this new contentNib I drag on a tableView and set File's Owner to the TableViewController. I then link the dataSource and delegate to file's owner (which is TableViewController).
Surprisingly this does not work and I receive the error:
**Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x3b0f910'**
This is bewildering to me since the file's owner is the TableViewController, which has been assigned to be both the dataSource and delegate. Does someone have either insight into my confusions, or a link to an example of how to have a compound view include a tableView?
*****update**** I see this in the Apple TableView programming guide:
"Note: You should use a UIViewController subclass rather than a subclass of UITableViewController to manage a table view if the view to be managed is composed of multiple subviews, one of which is a table view. The default behavior of the UITableViewController class is to make the table view fill the screen between the navigation bar and the tab bar (if either are present)."* <----- I don't really get what this is telling me to do though... if someone can explain or point me to an example I'd be much appreciated!
The File's Owner should be a class that you instantiate. For example, in your contentNib file, you should alloc and init a table view, passing the #"contentNib" as the argument to initWithNibNamed:bundle:. It sounds like you're not doing this, as the File's Owner, while set in IB to a UITableViewController, is actually just a UIViewController.
The setting in IB is just to let IB know what outlets and actions to make available; you are responsible for providing the proper object.

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.