Ok, this is really really easy, but even though I went through 100s of tutorials, I still have no idea whats wrong with my app. All I want to do is just to display a table view, even empty, but I get a black screen on the simulator and an exception in the output too.
This is what I did (followed step by step a few tutorials):
Open a view based app
This is my header:
#interface TableViewsViewController : UIViewController <UITableViewDelegate> {
IBOutlet UITableView *tblSimpleTable;
}
#property (nonatomic, retain) IBOutlet UITableView *tblSimpleTable;
#end
did not do anything in .m besides synthesizing.
IB: made 3 connections: delegate, dataSource, and tblSimpleTable to File's owner.
Yes, I am a beginner, but this is ridiculous...appreciate any help. Thanks!
First off, a table view has two "delegate" types -- the table view delegate and the table view data source. Your interface is being a delegate, but not a data source, for the table view.
If you add UITableViewDataSource to the interface, i.e.
#interface TableViewsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
and then compile, you'll probably get errors about missing methods for number of sections and for cell. After you add these to your implementation, the table view should work.
My guess is the table view tries to ask your class for number of sections in the table, and since your class doesn't actually respond to that selector, the code crashes.
Add
tblSimpleTable.delegate = self
in viewDidLoad
...and ensure you have connected it correctly in interface builder.
Related
I'm working on an Iphone project. In the storyboard I have myViewController containing few UILabels and a UITableView.
The mentioned UITableView is referenced in the myViewController class by an IBOutlet :
IBOutlet UITableView *myTable;
Now I build a UITableViewController (myTableViewController) that I want to populate myTable.
Then I go back to myViewController and I allocate in here myTableViewController :
MyTableViewController *myTableViewController = [[MyTableViewController alloc] init];
myTableViewController.tableView = myTable;
myTableViewController.tableView.delegate = myTableViewController;
myTableViewController.tableView.dataSource = myTableViewController;
It is not working (EXC_BAD_ACCESS error).
Usually I should populate myTable trough the UIViewController (making sure it conforms to nedeed protocols). But I'm really wondering If I can do what I did right above..
Most cases you can solve this by creating a UIViewController and a reference to the table inside the ViewController using an IBOutlet. However, there are cases where you need a full blown UITableViewController, such as in core data, and you also would like the ability to add things like UIToolbars. I know, a lot of times you can hack a UINavigationController, but sometimes you don't want a hierarchy/stack. In such a case, admittedly fairly rare, one way to do this is to create a UITableViewController subclass. You can import and initialize it in your UIViewController class, create an outlet from the tableView sitting inside the UIViewController's nib/scene, and then assign the tableView property of the UITableViewController to the tableView outlet of the UIViewController.
self.instanceOfUITableVC.table = self.tableView
It's a bit complex, but it works. Now you can control the tableView using the UITableViewController subclass instance.
I now want to refer to the table, but there isn't a variable in my .h file as it wasn't required when originally created. The table works great, can add/display/etc.
I tried adding "myTable" to the .h and then linking in IB (the table was linked to "view" and I could/did select "myTable"), but my first try didn't work and I was afraid of messing up my (somewhat) working app ;-)
Hope the question makes sense!!!
thx!
Simply use
IBOutlet UITableView * myTableView;
in your .h file i.e Declaration file.You can now access myTableView in IB.You can now create the object for this TableView.
If I understand you correctly, you want to link the table with your code. If that's the case, then in your header file you need to add a property like so
#property(nonatomic, retain) IBOutlet
UITableView *myTable;
And then synthesize it in your .m file like so
#synthesize myTable;
Once you've done that you will be able to link it up in interface builder. But bare in mind that if you want to populate/interact with the table further you need to set the data source/delegate of your table to your file's owner. Here is a tutorial on how to do that - http://www.switchonthecode.com/tutorials/how-to-create-and-populate-a-uitableview
Does that answer your question?
If your controller is a UITableViewController, just use its tableView property.
The question doesn't entirely make sense because it's not clear what "the table works great..." means if you don't have any reference to it in your .h file. I'll assume you do have an iVar of class UITableView that is defined as an IBAction and you've implemented the table view dataSource and delegate protocols in your view controller implementation. If you have not then the table cannot display any data.
First, if you have an iVar defined you can just refer to the iVar directly. If you prefer a property you can create one.
If your view controller is a UITableViewController you don't need to do anything, the property tableView is already defined for you and you can refer to the table with the tableView property.
If your view controller is something else, you can create a property as follows in your .h file:
#property (nonatomic, retain) UITableView * myTableView;
and in your .m
#synthesize myTableView;
You can then refer to your tableView with:
self.myTableView;
Replace myTableView with the name of your tableView iVar. If you need to connect your tableView in IB to the myTableView property then instead define the property as:
#property (nonatomic, retain) IBOutlet UITableView * myTableView;
Once you've done this, edit your UI in IB and connect the tableView outlet to the myTableView property. You will also need to assign the tableView dataSource and delegate correctly. This is getting a bit beyond your question. Apple's Table View Controller Programming Guide has great docs on this.
I have the following problem. I've created a ViewController pretty much like the above
#interface MyViewController : UIViewController {
IBOutlet UITableView *myTableView;
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
I've linked myTableView on the Interface Builder to the matching nib's UITableView. and I've subclassed MyViewController to create YourViewController as so:
#interface YourViewController : MyViewController {
}
And then I load from a TabBarController the YourViewController on a tab item. Although I can see that MyViewController is indeed invoked at the end, no table view is displayed on the emulator.
I've tried debugging the MyViewController and it appears the the IBOutlet is nil.
Why is that?
I have encountered major issues with inheritance and IBOutlets and IBAction. I advise you to avoid that, and create shared stuff in another way.
I was hit hard by bugs when getting memory warnings for instance. Outlets and actions didn't get reconnected properly when they were defined in base class vs derived class.
Probably MyViewController's nib file is not loaded at all. Are you using for YourViewController a specific nib file? and in which way are you creating YourViewController.
Can you also try to define an "awakeFromNib" method in YourViewController and then from it call [super awakeFromNib] ?
However to understand your issue you must clearly explain how you load objects and if and where you use Nibs?
If you add it dynamically using code then only it will work. Not using Nib.
the UITableView (ie. your myTableView) needs delegates for data source and and its control.
And your controller needs a link to the Table view in the xib.
declare table delegate protocols in the interface section of your controller.
using IB, link the TableView in your xib to owners file:delegates.
using IB, link the file owner myTableView to the tableView in the xib.
I hope it will help.
Assuming that you have your whole navigation stack in MainWindow.xib
Open MainWindow.xib
Select YourViewController (Or whatever your controller that is subclassing UITableViewController is called)
Select Attributes Inspector
Ensure that the 'NIB Name' property has your correct YourViewController (Or whatever the name) selected
I had this exact same issue, this solved it for me.
I am facing an issue and that is -
I am using tabBarCpntroller in my application. In this there are just two views. Both the views have tables in that.
I am able to display and populate the table in the first view but as I try to connect the delegate of UITable in the IB for the second view and then run the code, it crashes.
PS: I am using the same code as in first view.
Kindly help me out with the suggestions, I would be highly grateful
Thanks in advance!!
By the looks of your question, you need UITableView, not UITable, if this is what you are using, then you will have problems.
in your header, you must define your UITableView as an IBOutlet.
.h
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *myTable;
}
#property (nonatomic, retain) UITableView *myTable;
#end
Once you have done this, build (only) your code, then Interface Builder will see that outlet and you can connect it.
I am new to the iPhone SDK and am trying to create 3 views and switch between them. Data will come from a server and I will basically be showing 1 view and caching the other two. So far I am just trying to create a view and display it at run-time. My code is listed below. It shows only a blank screen and I think I am missing a key concept. Any Help?
#import <UIKit/UIKit.h>
#import "ImageViewController.h"
#interface Test5ViewController : UIViewController
{
IBOutlet UIView *rootView;
ImageViewController *curImage;
ImageViewController *nextImage;
ImageViewController *prevImage;
}
#property(nonatomic,retain) IBOutlet UIView *rootView;
#property(nonatomic,retain) ImageViewController *curImage;
#property(nonatomic,retain) ImageViewController *nextImage;
#property(nonatomic,retain) ImageViewController *prevImage;
#end
and
- (void)loadView
{
self.curImage = [[ImageViewController alloc]initWithNibName:#"ImageView" bundle:[NSBundle mainBundle]];
UIImage *pic = [UIImage imageNamed:#"baby-gorilla.jpg"];
[self.curImage assignImage:pic];
self.rootView = self.curImage.view;
}
and
#import <UIKit/UIKit.h>
#interface ImageViewController : UIViewController
{
IBOutlet UIImageView *image;
}
-(void)assignImage:(UIImage *)screenShotToSet;
#property(nonatomic,retain) IBOutlet UIImageView *image;
#end
Welcome to the iPhone SDK!
In general, there are two ways to get any view displayed.
First, and most commonly, you use a NIB file created by the Interface Builder. This is usually the easiest way to get started and I would recommend it for what you're trying to do here. It's too lengthy to describe all the steps you need to do for what you have here, but basically start in xcode by creating a new file and selecting "user interfaces" and choose View XIB. This will create a basic NIB file (they're called NIBs rather than XIBs for historical reasons). The first step in interface builder is to change the class name of the "File's Owner" to your UIViewController subclass (Test5ViewController). You can then drop anything that IB will allow into the view window or even replace the pre-supplied view object with one of your own. And here's the trick: make sure the view outlet (supplied by the UIViewController superclass) is connected to a view. Once this is done, this view will be automatically loaded when your NIB is loaded. You can then just put your UIViewController subclass (Test5ViewController) in your MainWindow.xib NIB file to get it automatically loaded, and you're in business.
Now, the way you're doing it here is the second way. Some people like to code this way all the time and not user interface builder. And while it's definitely necessary sometimes and always more flexible, it makes you understand what is happening a bit better. There may be other things, but the main thing you're missing is that in your code above, you have nothing that is adding your view into the view hierarchy. You need to check first that you have an UIApplicationDelegate subclass and it needs to load your "root" UIViewController class. All initial project creation types in xcode do this (except Window-based application). It is code like:
[window addSubview:rootController.view];
Once this is done, if your view controller wasn't loaded by the NIB (described briefly above), your loadView method will be called, expecting you to build your own view hierarchy. Above, you created the view(s), but failed to put them in a hierarchy. You need something like:
[self.view addSubview:curImage.view];
No view will be rendered until added to the view hierarchy. Make sure to look up the UIView class in the documentation and understand the variety of ways to add and remove views to the view hierarchy.
A couple things I should warn you about:
* your code above is leaking. You need to review how objective-C properties work. There's lots on this site about it. More than I have time to write about here.
* don't create a rootView property in the case you have here. There already is one in the superclass (UIViewController). It's just 'view'. Use that for saving your root view.
I hope this helps you get started. It can be bewildering at first, but you'll soon get it going! I recommend building and rewriting and rebuilding a lot of sample code before you do your "real" application. The SDK has many great samples.