无法在UITabBarController中显示辅助UITableViewController

时间:2010-04-09 09:13:00

标签: iphone objective-c uinavigationcontroller uitabbarcontroller uitableview

我以编程方式创建了一个UITabBarController加载到我的App Delegate中,如下所示:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
tabBarController = [[UITabBarController alloc] init];

myTableViewController = [[MyTableViewController alloc] init];
UINavigationController *tableNavController = [[[UINavigationController alloc] initWithRootViewController:myTableViewController] autorelease];
myTableViewController.title = @"Tab 1";
[myTableViewController release];

mySecondTableViewController = [[MySecondTableViewController alloc] init];
UINavigationController *table2NavController = [[[UINavigationController alloc] initWithRootViewController:mySecondTableViewController] autorelease];
mySecondTableViewController.title = @"Tab 2";
[mySecondTableViewController release];

tabBarController.viewControllers = [NSArray arrayWithObjects:tableNavController, table2NavController, nil]; 

[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}

现在我遇到的问题是我可以进入视图没问题,但是当我尝试单击表视图中的任何项目时,我无法在任何选项卡中显示辅助表视图。标签工作非常好,只是辅助视图。我在myTableViewController中使用下面的代码在选择特定行时运行(代码到达HELP行,然后崩溃)......

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
    // this gets the correct view controller from a list of controllers
SecondaryViewController *svc = [self.controllers objectAtIndex:row];

    /*** HELP NEEDED WITH THIS LINE ***/    
[self.navigationController pushViewController:svc animated:YES];

}

简单地说,我正在尝试将视图切换到新的视图控制器,同时保持标签可用并使用导航来回(如在iTunes应用程序中)。

2 个答案:

答案 0 :(得分:0)

您没有在问题中包含初始化self.controllers数组的方法。

我怀疑这个数组没有填充初始化的SecondaryViewController对象。

编辑(添加了适用于我的代码示例):

.h文件:

@interface FirstLevelViewController : UITableViewController {
    NSArray *controllers;
}
@property (nonatomic, retain) NSArray *controllers;
@end

和.m文件:

@implementation FirstLevelViewController
@synthesize controllers;
- (void)viewDidLoad {
    self.title = @"First Level";
    NSMutableArray *array = [[NSMutableArray alloc] init];

    // Disclosure Button
    DisclosureButtonController *disclosureButtonController =
    [[DisclosureButtonController alloc] 
     initWithStyle:UITableViewStylePlain];
    disclosureButtonController.title = @"Disclosure Buttons";
    disclosureButtonController.rowImage = [UIImage 
                                           imageNamed:@"disclosureButtonControllerIcon.png"];
    [array addObject:disclosureButtonController];
    [disclosureButtonController release];  

    // deleted further adds to array ...

    self.controllers = array;
    [array release];
    [super viewDidLoad];
}

答案 1 :(得分:0)

简单地说,我正在创建控制器数组,self.controllers,添加对象,然后释放对象。

如果在释放数组之前没有释放对象,它似乎没有问题。

相关问题