0
votes

I added a UITableView to a storyboard file in Xcode and the corresponding .m UIViewController (set in the attribute inspector panel as the class type). I have an instance variable myTableView in the UIViewController, which I wanted to assign to the storyboard's UITableView to in order to call the method reloadData in code.

However when I try printing out the variable info within the init or viewDidLoad method with NSLog of the UIViewController, the variable is null.

Example code from the UIViewController: @property (nonatomic, strong) IBOutlet UITableView *myTableView;

I am initializing the corresponding UIViewController as follows:

self = [super initWithNibName:@"CorrespondingNibViewControllerName" bundle:nil];

I had hoped the myTableView instance variable was already initialised to the UITableView via the storyboard but this doesn't seem to be happening...

2
Add a IBOutlet for the tableview in your .h file and hook it up in your xib file.7c9d6b001a87e497d6b96fbd4c6fdf

2 Answers

0
votes

You are correct to assume that assigning the tableview in numberOfRows is not the way it should be done.

You should simply assign it via the IBOutlet via Interface Builder instead and it will automatically be assigned for you without any extra code.

0
votes

I would create two new files myTableView.h and myTableView.m subclass of UITableView. Then in .xib file, click on your newly added UITableView object and assign custom class to myTableView on Identity Inspector panel.

In your own subclass.m of UIViewController you can instantiate myTableView object in prepareForSeque: sender: method. Also you can add the code you want to print in viewDidLoad under your UIViewController subclass.m.

Don't forget to assign a name for your Segue Identifier in Attribute Inspector.