Core data IOS模态视图控制器显示黑屏
我正在开发一个IOS应用程序,已经被这个问题困扰了大约一周了,现在找不到解决方案。如果您能提供任何帮助,我们将不胜感激。以下是我的设置:Core data IOS模态视图控制器显示黑屏,core-data,Core Data,我正在开发一个IOS应用程序,已经被这个问题困扰了大约一周了,现在找不到解决方案。如果您能提供任何帮助,我们将不胜感激。以下是我的设置: 我有一个选项卡栏控制器 我有一个TableViewController,它有一个带有导航项“Add”的导航栏 在你按下“添加”选择器后,我会展示另一个带有选择器的viewController 我使用的是核心数据 当第二个视图控制器以模式显示时,它会出现一个带有导航栏的黑屏。如果我从一个不相关的屏幕访问第二个视图控制器,没有导航栏的情况下,它会正常运行 没有记录
- (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];
}
以下是我的第二个视图控制器中的代码:
- (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];
}
任何关于今后走向的建议都会非常有用 我猜您初始化的方式不对:
SecondViewController *addController = [[SecondViewController alloc] init];
应该是
initWithNIB:
在经历了许多挫折之后,我找到了答案。如果使用的是情节提要,则无法按标准代码导航到下一个视图控制器。我将prepareForSegue语句放在TableViewController.m文件中,然后将连接连接到情节提要上,并确定了该段
现在,当您按下Add按钮时,它将进入new view controller屏幕,而不是黑色
当我尝试initWithNib时:我收到一条错误消息由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:“无法在捆绑包中加载NIB”。我想这是因为我使用的是故事板。更新:我尝试添加另一个只有文本字段的视图控制器,并将其替换为带有选择器的视图控制器,但仍然出现相同的黑屏。有人知道用不同的方法来初始化第二个视图控制器吗?这似乎是问题所在。只需在故事板中使用一个序列,从任何触发模式显示的操作(如从按钮或表格单元格到模式视图控制器)拖动ctrl键即可。谢谢您的建议,但我已经尝试过了,但它没有任何作用。我认为问题出在启动SecondViewController的线路上。我在这个示例项目上也面临类似的问题。感谢您的帮助。