2012-01-10 74 views
0

我正在一个IOS应用程序,并已被这个问题困扰了大约一个星期,现在无法找到解决方案。任何帮助你可以提供将不胜感激。这是我的设置:IOS莫代尔视图控制器呈现黑色屏幕

  1. 我有一个标签栏控制器。
  2. 我有一个TableViewController,它有一个导航栏,导航条目“添加”。
  3. 按下“添加”选择器后,我模态地呈现另一个viewController,它有一个选择器。
  4. 我正在使用核心数据。

当第二个视图控制器被模态地呈现时,它会出现一个带有导航栏的黑色屏幕。如果我从一个不相关的屏幕模态地访问第二个视图控制器,它会在没有导航栏的情况下正常显示。

没有记录错误消息,甚至没有当您按导航栏上的“保存”时未保存该对象。但是,按下“保存”会将您带回到TableViewController,它看起来像添加了实体。

这里是我的TableViewController代码:

- (void)add:(id)sender { 
    SecondViewController *addController = [[SecondViewController alloc] init]; 
    addController.delegate = self; 

    Entity *newEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:self.managedObjectContext]; 
    addController.entity = newEntity; 

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addController]; 
    [self.navigationController presentModalViewController:navController animated:YES]; 
} 

- (void)secondViewController:(SecondViewController *)secondViewController didAddEntity:(Entity *)entity { 

    if (entity) {   

    [self showEntity:entity animated:NO]; 
} 

[self dismissModalViewControllerAnimated:YES]; 
} 


- (void)showEntity:(Entity *)entity animated:(BOOL)animated { 
    EntityDetailTableViewController *detailViewController = [[EntityDetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
    detailViewController.entity = entity; 

    [self.navigationController pushViewController:detailViewController animated:animated]; 
} 

这里是我的第二个视图控制器代码:

- (void) save { 

    entity.attribute = attributeTextField.text; 

    NSError *error = nil; 

    if (![entity.managedObjectContext save:&error]) 
     { 
     NSLog(@"Problem saving attribute: %@", [error localizedDescription]); 
     } 
     NSLog(@"saveAttribute"); 

    [self.delegate secondViewController:self didAddEntity:entity]; 

} 

在哪里何去何从将是真正有用的任何建议。

回答

2

太多的无奈之后,我找到了答案。如果您正在使用故事板,则无法通过标准代码导航到下一个视图控制器。我把prepareForSegue语句放在我的TableViewController.m文件中,然后连接故事板上的连接并确定了segue。

现在,当您按下添加按钮时,它会跳到新的视图控制器屏幕,并且它不是黑色的。

Here's a link to a useful tutorial

0

我的猜测是要初始化的方法是不对的:

SecondViewController *addController = [[SecondViewController alloc] init]; 

应该initWithNIB:

+0

当我尝试initWithNib:我得到终止应用程序的错误信息”由于未捕获的异常‘NSInternalInconsistencyException’,原因是:‘在包无法加载NIB’我想这是因为我用故事板 – PopUp 2012-01-11 04:30:12

+0

更新:我尝试添加另一个视图控制器,它只有一个textField,并将其替换为具有选择器的视图控制器,并且仍然遇到相同的黑屏。是否有人知道用不同的方法来初始化第二个视图控制器以模态方式使用Storyboard?这似乎是问题 – PopUp 2012-01-11 14:58:30

+0

只需在故事板中使用Segue,从任何操作触发拖动即可触发模式出现,例如从按钮或表格单元格到mo达尔视图控制器。 – agilityvision 2012-01-11 16:07:54