3

Here's the basic code (based on Xcode's Tabbed Applicaion Template)

ViewController.h

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,retain) NSMutableArray *movies;
@property (nonatomic,retain) IBOutlet UITableView *tableView;

ViewController.m

@implementation ViewController
@synthesize movies,tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Watchlist", @"Watchlist");
        self.tabBarItem.image = [UIImage imageNamed:@"watchlist"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     NSLog(@"tableView = %@", tableView);
}

Output

tableView = (null)

The TableView is connected to File's owner in IB with class is set to ViewController

I really don't get why the tableView is null. I'm not a complete newbie to Cocoa (but to the iPhone SDK), I created a Single View based Application with a TableView dataSource to see if I was missing something. I got it working in under a minute.

Anybody can help out?

Rene Koller
  • 375
  • 1
  • 6
  • 15
  • Are you trying to implement a Navigation controller? Why are you setting a title? – SushiGrass Jacob Jul 17 '11 at 14:51
  • No a normal View controller, also i left - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil untouched besides the title and the image names. – Rene Koller Jul 17 '11 at 15:26

2 Answers2

8

In interface builder right click File's Owner and ensure that the following connections are made:

Outlets
tableView - Table View

Referencing Outlets
dateSource - Table View
delegate   - Table View

I suspect you may have not made the first connection?

Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • 1
    Made all connections, as I said i got it working with a Singe View based Application easily. I think i read somewhere this can happen when using - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil but after 30 min of search I opened this because I could not find it. – Rene Koller Jul 17 '11 at 15:23
  • 2
    Are you using the correct nib when you init the viewController? I often put the nib selection logic in the viewController itself as it make's no sense that another object should have a better idea what nib a class should load. If not when you init make sure you pass the correct nib name to use – Paul.s Jul 17 '11 at 15:36
  • Ah you are my hero! I recently refactored the View Controller and of course the strings did not refactor. – Rene Koller Jul 17 '11 at 15:40
1

Make sure the nib/xib is included in your current target.

I just experienced this issue on Xcode5 with iOS7 SDK. Strangely enough, I discovered that the nib wasn't included in my target anymore. I don't know when and why that happened, but it had this strange side effect that most IBOutlets weren't set up properly, even if all connections from code to nib/xib were fine.

For example: my MKMapView *map was in my list of subviews on viewDidLoad:, but the IBOutlet MKMapView *map property in my view controller was still nil. After I ticked the "include in target" checkbox, everything worked as expected.

auco
  • 9,329
  • 4
  • 47
  • 54