ios - 从 xib 文件中以模态方式呈现 View Controller

标签 ios objective-c uiviewcontroller presentmodalviewcontroller

我有一个与 UIViewController 自定义类 (ZocaloPlayerViewController) 关联的 xib 文件

enter image description here

我正在尝试通过水平翻转模态呈现 UIViewController(在 Storyboard 中制作)

enter image description here

但是我得到一个错误:

这就是我试图呈现 View Controller 的方式

ShowDetailsArtistAlbumViewController *vc = [[ShowDetailsArtistAlbumViewController alloc] initWithNibName:@"ShowDetailsArtistAlbumViewController" bundle:nil];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:^{

}];

但是我收到这个错误...

Uncaught exception: Could not load NIB in bundle: 'NSBundle </var/mobile/Containers/Bundle/Application/57C2AD20-51DB-4C43-BB8F-2E8F22CD2E2A/MyMusicApp.app> (loaded)' with name 'ShowDetailsArtistAlbumViewController'

我正在尝试

[self.parentViewController presentViewController:vc animated:YES completion:^{

}];

但是没有任何反应......

编辑: 我试过这种方式

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ShowDetailsArtistAlbumViewController *vc = [sb instantiateViewControllerWithIdentifier:@"ShowDetailsArtistAlbumViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:^{

}];

显示了 View Controller ,但我得到的是黑色 View ,但内部什么也没有

最佳答案

你说“我有一个关联的 xib 文件”,但问题是你没有。运行时正在寻找名为 ShowDetailsArtistAlbumViewController.xib> 的 xib 文件,但您没有。您需要重命名您的 xib 文件以匹配运行时的期望。

或者,您可能错误地创建了 View Controller 。你是这样说的:

ShowDetailsArtistAlbumViewController *vc = 
    [[ShowDetailsArtistAlbumViewController alloc] 
        initWithNibName:@"ShowDetailsArtistAlbumViewController" bundle:nil];

这就是为什么运行时认为您有一个名为 ShowDetailsArtistAlbumViewController.xib 的 nib 的原因 - 您告诉它,就在那一行!

如果实际上有一个名为 ShowDetailsArtistAlbumViewController.xib 的 nib,那很好。你的问题是没有。你对运行时撒了谎,所以它自然会崩溃。

如果您确实想在 Storyboard 中使用 View Controller ,那不是如何访问该实例。您正在做的是创建一个不同的 实例。如果您想要 Storyboard 中的实例,您需要与 Storyboard 对话并将其告诉 instantiateViewControllerWithIdentifier:

关于ios - 从 xib 文件中以模态方式呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35733133/

相关文章:

iphone - self.navigationController 和 delegate.navigationController 有什么区别

ios - appdelegate swift4 中的全局变量

ios - 在ios中保持view controller的第一个状态

ios - Swift 将键路径作为函数的参数传递

ios - UILocalNotification 的 UserInfo 始终为 nil

c - 有没有办法从可变参数函数的参数中检索 float ?

objective-c - 更改 iOS 中的坐标方向

ios - 跨多个 View Controller 使用共享背景 View 的最佳实践是什么?

objective-c - 将对象添加到 NSMutableArray 时出现问题

ios - 将 segue 从 ViewController 推送到 TableViewController 不工作