【问题标题】:UIViewController not loading a UIViewUIViewController 没有加载 UIView
【发布时间】: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 文件中链接并工作。所以你,我有点坚持这个,任何帮助都会很棒!

谢谢大家

【问题讨论】:

    标签: iphone uiview nsarray


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 2011-06-14
      • 2015-09-06
      相关资源
      最近更新 更多