【发布时间】:2010-03-02 22:17:47
【问题描述】:
嘿,我正在玩我老师为基于表格的应用程序提供的脚本。但是我似乎无法加载自己的视图。
文件:
- SubViewOneController(这是一个子视图,也有一个nib)
- TapViewController(我创建并希望添加到单元格的自定义 UIView)
- RootViewController(在视图中加载的主控制器)
- SimpleNavAppDelegate
它是如何工作的:在 RootViewController 中,有一个 NSArray 包含在 -(void)awakeFromNib {} 方法中声明的 NSDictionary 对象
- (void)awakeFromNib {
// we'll keep track of our views controllers in this array
views = [[NSMutableArray alloc] init]; // when using alloc you are responsible for it, and you will have to release it.
// ====================================================================================================
// ====================================================================================================
// LOADING IN CUSTOM VIEW HERE:
// allocate a set of views and add to our view array as a dictionary item
TapViewController *tapBoardView = [[TapViewController alloc] init];
//push onto array
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
@"Tab Gestures", @"title",
tapBoardView, @"controller",
nil]];
[tapBoardView release]; //release the memory
// ====================================================================================================
// ====================================================================================================
SubViewOneController *subViewOneController = [[SubViewOneController alloc] init];
// This will set the 2nd level title
subViewOneController.title = @"Swipe Gestures"; //set it's title
//push it onto the array
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
@"Swipe Gestures", @"title",
subViewOneController, @"controller",
nil]];
[subViewOneController release]; //release the memory
}
稍后我设置表格视图:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// OUTPUT -- see console
NSLog(@"indexPath %i", indexPath.row); // OUTPUT: tapController: <TapViewController: 0x3b2b360>
NSLog(@"view object: %@", [views objectAtIndex:indexPath.row]); // OUTPUT: view object: controller = <TapViewController: 0x3b0e290>; title = "Tab Gestures";
// ----- Hardcoding the controller and nib file in does work, so it's not a linkage issue ------
// UNCOMMENT TO SEE WORKING -- comment below section.
//TapViewController *tapContoller = [[TapViewController alloc] initWithNibName:@"TapBoardView" bundle:nil];
//NSLog(@"tapController: %@", tapContoller);
//[self.navigationController pushViewController:tapContoller animated:YES];
// ----- Random Tests -----
//UIViewController *targetViewController = [[views objectAtIndex: 0] objectForKey:@"controller"]; // DOES NOT WORK
// LOADS THE SECOND CELL (SubViewOneController) however will not load (TapViewController)
UIViewController *targetViewController = [[views objectAtIndex: indexPath.row] objectForKey:@"controller"];
NSLog(@"target: %@", targetViewController); // OUTPUT: target: <TapViewController: 0x3b0e290>
[self.navigationController pushViewController:targetViewController animated:YES];
}
阅读 cmets 您应该能够看到硬编码视图是有效的 - 但是尝试从视图 NSArray 加载它是行不通的。然而,它确实包含内存中的对象,看到 NSLog 证明了这一点。
一切都在 TapViewController nib 文件中链接并工作。所以你,我有点坚持这个,任何帮助都会很棒!
谢谢大家
【问题讨论】: