I want to make Navigation with 3 levels. I make the root controller and fill the table with data. But when I touch on some cell my application crash. This is part of my code:
NavAppDelegate.h
#import <UIKit/UIKit.h>
@interface NavAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
NavAppDelegate.m
import “NavAppDelegate.h”
@implementation NavAppDelegate
@synthesize window=_window;
@synthesize navigationController=_navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_window release];
[_navigationController release];
[super dealloc];
}
@end
RootViewController.h
#import <UIKit/UIKit.h>
@class SubCategory;
@interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
SubCategory *subCategories;
}
@property (nonatomic, retain) SubCategory *subCategories;
@end
RootViewController.m
#import "RootViewController.h"
#import "SubCategory.h"
#import "OffersViewController.h"
@implementation RootViewController
@synthesize subCategories;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Sub Categories";
NSString *jsonArray = [NSString stringWithFormat:@"{ "
@" \"sub-categories\": { "
@" \"parent\": \"1\", "
@" \"count\": \"2\", "
@" \"sub-category\": [{ "
@" \"id\": \"1\", "
@" \"name\": \"Buy\" "
@" }, "
@" { "
@" \"id\": \"2\", "
@" \"name\": \"Sell\" "
@" }] "
@" } "
@" }"];
SubCategory* categories = [[SubCategory alloc] init];
[categories parseJSON:jsonArray];
subCategories = categories;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [subCategories.subCategoryName count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewController * offers = [[OffersViewController alloc] initWithNibName:@"OffersView" bundle:nil];
//offers.title = [NSString stringWithFormat:@"%@", [subCategories.subCategoryName objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:offers animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachedCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] init] autorelease];
}
cell.textLabel.text = [subCategories.subCategoryName objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
@end
OffersViewController.h
#import <UIKit/UIKit.h>
@interface OffersViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
}
@end
OffersViewController.m
#import "OffersViewController.h"
@implementation OffersViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ // Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachedCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] init] autorelease];
}
cell.textLabel.text = @"niki";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
The exception is:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "OffersView" nib but the view outlet was not set.'
In your file’s owner of your xib file set the view outlet to the view.
OffersViewController‘s view is not set in your case.Search for view outlet in OffersViewController File owner and set it to the view in Interface builder as shown.