 |
 |
|
 |
08-29-2008, 09:54 AM
|
#1 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 17
Rep Power: 0
|
Problem Creating a TableView in a TabBar App
Hey everyone, I'm at my wits end with this, and hopefully someone can help. I think this must be an Interface Builder bug, but let me know what you think and if you know a way around it.
Whenever I try to create a View in a tab bar based application that contains a Table View, I end up with the following exception when trying to switch to the tab:
'NSUnknownKeyException', reason: '[<UIViewController 0x451100> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
If I create a simple view based application with a Table View, everything works fine. I'll show the steps I go through below for both cases and hopefully someone can see what I'm doing wrong/differently that I don't realize.
Case 1:- In Xcode go to File...New Project...View Based Application, and name it Demo.
- Open DemoViewController.h and add the following code in the appropriate two locations:
Code:
IBOutlet UITableView *tableView;
@property (nonamtomic,retain) UITableView *tableView;
- Open DemoViewController.m and add the following line:
Code:
@synthesize tableView;
- Open DemoViewController.xib
- Drag a Table View into the view
- Right click on the table view.
- Drag from the dot beside New Referencing Outlet to File's Owner and select tableView.
- Save everything and Build and Go. The table view shows up fine in the simulator.
Case 2:- In Xcode go to File... New Project ... Tab Bar Application and name it Demo2.
- Right click on Classes...Add...New File...UIViewController subclass and name it SecondViewController.
- Open SecondViewController.h and add the following code in the appropriate two locations:
Code:
IBOutlet UITableView *tableView;
@property (nonamtomic,retain) UITableView *tableView;
- Open SecondViewController.m and add the following line:
Code:
@synthesize tableView;
- Open SecondView.xib
- Remove the label that says Second View
- Select File's Owner and in the Inspector Identity tab change the Class to SecondViewController.
- Drag a Table View into the view
- Right click on the table view.
- Drag from the dot beside New Referencing Outlet to File's Owner and select tableView.
- Save everything and Build and Go.
The first view shows up fine. Click on the Second tab. The simulator crashes with the following stack trace on the console:
[Session started at 2008-08-29 08:10:45 -0600.]
Loading program into debugger…
Warning - No location found for "DatabaseSearchViewController.m:53"
GNU gdb 6.3.50-20050815 (Apple version gdb-960) (Sun May 18 18:38:33 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".warning: Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit" (file not found).
warning: Unable to read symbols from "UIKit" (not yet mapped into memory).
Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/myusername/Library/Application Support/iPhone Simulator/User/Applications/662568B1-441A-4705-9003-0BF0D68D45C1/Demo2.app/Demo2', process 64174.
2008-08-29 08:10:50.011 Demo2[64174:20b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x451100> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
2008-08-29 08:10:50.012 Demo2[64174:20b] Stack: (
2535018827,
2522071291,
2535017585,
2490358200,
2490356766,
2490899809,
818073927,
2534972549,
818068629,
818076920,
816648466,
816648934,
816688492,
816684429,
816177758,
818091155,
816177758,
816558114,
816559338,
816558227,
816177758,
816558114,
816559338,
816556080,
816274443,
816209415,
816206378,
829003042,
829012108,
2534520341,
2534522104,
829005112,
829005309,
816175835,
816221412,
9864,
9718
)
|
|
|
08-30-2008, 06:47 AM
|
#2 (permalink)
|
|
Member
Join Date: Aug 2008
Posts: 81
Rep Power: 1
|
You are missing a few steps here. I think your main mistake is this:
"Right click on Classes...Add...New File...UIViewController subclass and name it SecondViewController."
You should be adding a new UITableViewController subclass instead. It has a tableView field already and it is automatically hooked up to the first table in the xib you don't need to hook it up. You do however need to connect the delegate and the data source to the file's owner in the xib, and of course connect the file owner's view to the table .
Watch my video carefully which has 2 tables in a tab bar.
|
|
|
08-30-2008, 09:29 PM
|
#3 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 17
Rep Power: 0
|
Quote:
Originally Posted by indiekiduk
You are missing a few steps here. I think your main mistake is this:
"Right click on Classes...Add...New File...UIViewController subclass and name it SecondViewController."
You should be adding a new UITableViewController subclass instead. It has a tableView field already and it is automatically hooked up to the first table in the xib you don't need to hook it up. You do however need to connect the delegate and the data source to the file's owner in the xib, and of course connect the file owner's view to the table .
Watch my video carefully which has 2 tables in a tab bar.
|
Thanks, this helped. I left the datasource and delegate out of the example because they were irrelevant to showing the exception. Following your example I can make things work, but I'm still not sure why the other way doesn't work. In my first example I am using a UIViewController and it works so why can't I do it in the second example?
More specifically, following your way only seems to work if the entire view is a table. I want a view in the second tab that has a search bar at the top, with a table taking the rest of the view and the only way IB lets me do that is if I am using a UIView, not a UITableView as the base view, and if I do that, I get errors saying the controller is expecting a UITableView. The only way to get rid of the errors is to switch the controller to a UIViewController (instead of UITableViewController as you suggest), which puts me right back at the spot I was in for my second demo.
I'm sure there's an easy way to do what I want, it just escapes me what it is. Is there a way to add another object to IB that I can connect my table view to? So I could have a UIViewController as the File's Owner, being the delegate and outlet for the search view, and then a second object that is a UITableViewController that is the delegate, datasource and outlet for the UITableView? Or am I trying to make things too complicated?
|
|
|
08-31-2008, 06:11 PM
|
#4 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 17
Rep Power: 0
|
Well, I have things working the way I want them, but I'm not entirely sure why. After doing more research and looking into the TableSearch example, I found that it appeared they did exactly what I did in Case 2 of my original post, except including a UISearchBar as well. I repeated essentially what I did in Case 2, and this time it worked. I obviously did something different, but I'm not entirely sure what (and no, I did not use a UITableViewController anywhere).
Since I don't really have the time to keep repeatedly trying this over and over to find out what I did different, my test project instantly got promoted to a production project and I will deal with what was different the next time I need a search bar and table view together again.
|
|
|
09-02-2008, 09:43 PM
|
#5 (permalink)
|
|
Junior Member
Join Date: Sep 2008
Posts: 8
Rep Power: 0
|
I spent hours dealing with this as well and this post was the first Google result. Anyway, for anyone else's reference, see Apple - Support - Discussions - Very basic problem with Tab Bar ... for solution.
In short:
Beside naming/locate the nib file, you must also set the class in the view inside the tab bar.
|
|
|
09-03-2008, 09:03 AM
|
#6 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 17
Rep Power: 0
|
Quote:
Originally Posted by amirbudicecep
|
Fantastic find! I just tested this with my test case and it worked first try. In my vast fiddling I must have set this without realizing it to get my code working. I'm glad I can now consistently add tables to tab bars.
Thank you!
|
|
|
09-05-2008, 12:47 AM
|
#7 (permalink)
|
|
Junior Member
Join Date: Sep 2008
Posts: 14
Rep Power: 0
|
nice video, but now i am stuck again =(
in the FirstViewController.m
how do you access the navigation controller?
I want to push in another view,
but not sure how to access the navigation controller
to use the method pushViewController
thanks
sorry, totally new to iphone sdk =(
|
|
|
09-05-2008, 02:13 AM
|
#8 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 13
Rep Power: 0
|
Hi indiekiduk, thanks. It's helpful for me.
I have created two tab with table view. But i got a problem to add a table item. In the first table view i added simply "Hello Universe" it's working. Then i added NSMutableArray on second table view. but now its not working*
FirstViewController.m - [cell setText:@"Hello Universe"] - it's working fine..
but if add NSArray to AppDelegate it's not working. did anything wrong in this??
TabDemoAppDelegate.m - array = [[NSArray arrayWithObject:@"First",@"Second",@"Third",nil] retain]
|
|
|
09-05-2008, 08:26 AM
|
#9 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 17
Rep Power: 0
|
Quote:
Originally Posted by rapidbunny
nice video, but now i am stuck again =(
in the FirstViewController.m
how do you access the navigation controller?
I want to push in another view,
but not sure how to access the navigation controller
to use the method pushViewController
thanks
sorry, totally new to iphone sdk =(
|
To access the navigation controller, you have to declare an IBOutlet for it in your controller class and connect it via IB the same way you connect any other outlet. Then you have direct access to it.
|
|
|
09-05-2008, 08:31 AM
|
#10 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 17
Rep Power: 0
|
Quote:
Originally Posted by dhanasekar
Hi indiekiduk, thanks. It's helpful for me.
I have created two tab with table view. But i got a problem to add a table item. In the first table view i added simply "Hello Universe" it's working. Then i added NSMutableArray on second table view. but now its not working*
FirstViewController.m - [cell setText:@"Hello Universe"] - it's working fine..
but if add NSArray to AppDelegate it's not working. did anything wrong in this??
TabDemoAppDelegate.m - array = [[NSArray arrayWithObject:@"First",@"Second",@"Third",nil] retain]
|
You haven't provided a lot of information here but going from what you have posted, you need to have the array declared in your table view's data source, and set the cell text from the appropriate location in the array in the method
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
09-05-2008, 08:42 PM
|
#11 (permalink)
|
|
Junior Member
Join Date: Sep 2008
Posts: 14
Rep Power: 0
|
hum for the first TAB i have
FirstViewController and a DetailedViewController
where should i put the navigation iboutlet at?
the projectdelegate? the FirstViewController or DetailedViewController?
if i put the navigation iboutlet at the first controller, how do i access navigationcontroller on the DetailedViewController then?
whats the usual design pattern for this? thanks
|
|
|
09-05-2008, 08:51 PM
|
#12 (permalink)
|
|
Junior Member
Join Date: Sep 2008
Posts: 14
Rep Power: 0
|
I am thinking it has to go on the AppDelegate?
so if i have two tabs.....
then i should have two navigation controller for each of the two tabs in the AppDelegate? is this the correct design?
|
|
|
09-05-2008, 09:02 PM
|
#13 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 17
Rep Power: 0
|
Actually, I played with it some more, and it appears you don't have to do any of the IBOutlet stuff, you can just use
Code:
[self navigationController]
to access the navigation controller containing the ViewController you are in.
The more I use it the more I hate using IB with the iPhone SDK. It is completely inconsistent in how things are done, with some requiring outlets and some not.
|
|
|
09-05-2008, 10:12 PM
|
#14 (permalink)
|
|
Junior Member
Join Date: Sep 2008
Posts: 14
Rep Power: 0
|
you are right, this works, any downside to doing this way?
what exactly does it mean when you call "self"?
is this similar to "this" in java? but then "this" in java only applies to the object you are on......what is "self" exactly referring to? the delegate?
|
|
|
09-05-2008, 10:24 PM
|
#15 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 17
Rep Power: 0
|
You are correct, "self" is the equivalent of "this" in Java, referring to the object you are in.
|
|
|
09-15-2008, 08:06 PM
|
#16 (permalink)
|
|
Junior Member
Join Date: Sep 2008
Posts: 8
Rep Power: 0
|
Quote:
Originally Posted by bhil
Actually, I played with it some more, and it appears you don't have to do any of the IBOutlet stuff, you can just use
Code:
[self navigationController]
to access the navigation controller containing the ViewController you are in.
The more I use it the more I hate using IB with the iPhone SDK. It is completely inconsistent in how things are done, with some requiring outlets and some not.
|
My initial impression is also similar. But after [re]reading the official guides for the nth times, they give more sense every time (the guides are definitely NOT the easiest or best-seller reading materials).
navigationController is a property of UIViewController. The following is quoted from UIViewController reference (requires Apple ID):
navigationController
A parent or ancestor that is a navigation controller. (read-only)
@property(nonatomic, readonly, retain) UINavigationController *navigationController
Discussion
Only returns a navigation controller if the view controller is in its stack. This property is nil if a navigation controller cannot be found. So, as long as the view controller is in the navigation controller's stack (being 'pushed' to the stack via pushViewController:animated: ), it will have a reference back to the navigation controller. Similar to this are tabBarController and parentViewController (it can be the controller that presents "self" as modal, or the navigation controller, or the tab bar controller, or nil). For these "parents", we don't need to create IBOutlets to refer to them.
Quote:
Originally Posted by rapidbunny
is this similar to "this" in java? but then "this" in java only applies to the object you are on......what is "self" exactly referring to? the delegate?
|
If you're using it inside the DetailedViewController class, then "self" is referring to its instance/object. DetailedViewController itself is a subclass of UIViewController. So, if it's been pushed to the navigation controller's stack previously, [self navigationController] (or self.navigationController) will refer to its "parent", i.e. the navigation controller.
|
|
|
03-22-2009, 11:40 AM
|
#17 (permalink)
|
|
Junior Member
Join Date: Aug 2008
Posts: 2
Rep Power: 0
|
details please
"Beside naming/locate the nib file, you must also set the class in the view inside the tab bar.[/quote]"
I am extremely sorry for sounding naive. Could you please tell me menu wise steps for setting these values ? I am still scratching my head how to set this. I am getting exactly the same error using a navigation based app
thanks and regards
Jagat
|
|
|
04-10-2009, 04:44 AM
|
#18 (permalink)
|
|
Senior Member
Join Date: Feb 2009
Location: Bangkok
Posts: 145
Rep Power: 1
|
indiekiduk: thanks for posting that video!
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
|
» Online Users: 262 |
| 25 members and 237 guests |
| amoll, bensj, BostonMerlin, crossfire, dapis, DGuy, Dracor, Groucho, heinrich, idigit, Jeremy1026, Jume, kaleman, lastiko, lildragon, mattjgalloway, mnemonic_fx, odysseus31173, Oliver Drobnik, Opticfibre, shabzcohelp, Sicga, tonylmiller, utopiaplan, wolkje |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 8,229
Threads: 20,197
Posts: 90,213
Top Poster: RickMaddy (2,121)
|
| Welcome to our newest member, jrsiqueira |
|