设置标题时导航控制器出现问题

时间:2011-05-31 07:09:44

标签: iphone uitableview uinavigationcontroller

我想用3个级别进行导航。我制作根控制器并用数据填充表格。但当我触摸某些单元格时,我的应用程序崩溃了。这是我的代码的一部分:

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;
}

例外是:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "OffersView" nib but the view outlet was not set.'

1 个答案:

答案 0 :(得分:1)

在文件的xib文件所有者中,将视图插座设置为视图。

OffersViewController的视图未在您的情况下设置。

在OffersViewController文件所有者中搜索视图出口,并将其设置为“界面”构建器中的视图,如图所示。

相关问题