UIViewController没有加载UIView

时间:2010-03-02 22:17:47

标签: iphone uiview nsarray

嘿,我正在玩一个我老师为基于表格的应用程序提供的脚本。但是我似乎无法自己加载视图。

文件:

  • SubViewOneController(子视图,也有一个笔尖)
  • 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];   

}

阅读您应该能够看到对视图进行硬编码的注释,可以正常工作 - 但是尝试从View NSArray加载它不起作用。但它确实包含了内存中的对象,看到NSLog证明了这一点。

在TapViewController nib文件中,所有内容都已链接并正常工作。所以,我有点坚持这个,任何帮助都会很棒!

谢谢你们

1 个答案:

答案 0 :(得分:0)

您在顶部使用了错误的TapViewController初始化方法。视图控制器上的普通旧init不会加载笔尖。假设您的笔尖正确连接(如您所说),如果您不加载笔尖,则视图控制器的view属性永远不会被设置为任何内容。所以view属性为零。因此,您的视图控制器没有任何视图显示您的应用何时使用它。

TapViewController *tapBoardView = [[TapViewController alloc] init];

而你在“硬编码”部分中使用了正确的部分:

TapViewController *tapContoller = [[TapViewController alloc] initWithNibName:@"TapBoardView" bundle:nil];