2011-05-31 100 views
0

我想用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

进口 “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 

RootVi ewController.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

在您的厦门国际银行文件集的文件的所有者视图出口到视图。

OffersViewController的视图没有在你的情况下设置。

搜索OffersViewController文件所有者视图出口并且如图所示将其设置为在接口构建器的图。

+0

非常感谢这项工作:))但现在我有另一个问题。我启用僵尸和它打印*** - [UITableViewController tableView:numberOfRowsInSection:]:消息发送到释放实例0x4e42be0'但我没有释放任何此对象 – 2011-05-31 07:30:24

+0

我希望你保留了你的表格视图。 – 2011-05-31 07:51:53