【问题标题】:Unable to show 2nd UIViewController from 1st within navigationController无法在 navigationController 中从第一个显示第二个 UIViewController
【发布时间】:2014-02-23 15:34:23
【问题描述】:

我有一个主故事板,其设置如下

--导航控制器(是初始视图控制器)

----Table View Controller(根视图控制器)-#1

----UI 视图控制器(segue 类型是来自 Nav.Ctrl 的 Modal) - #2

#1 连接到自定义类

#2 连接到不同的自定义类

在 #1 中,当从一个表格单元格中单击一个按钮时。我想调用 #2 来完成某些事情,并希望返回(通过单击后退按钮)到 #1

我尝试了几个不同的代码,没有运气!

以下代码会产生黑屏,但导航栏信息包含返回主页的选项

vcControl2 *popup = [[vcControl2 alloc] init];
popup.modalPresentationStyle = UIModalPresentationFormSheet;
[self.viewController.navigationController pushViewController:popup animated:YES];

下面的代码会产生带有空白导航栏信息的黑屏

vcControl2 *vc = [[vcControl2 alloc] initWithNibName: nil bundle:nil ] ;
UINavigationController * navcontroller = [[UINavigationController alloc] initWithRootViewController:vc];
[self.viewController presentModalViewController: navcontroller animated: YES];

下面的代码会产生没有导航栏信息的黑屏

vcControl2 *popup = [[vcControl2 alloc] init];
popup.modalPresentationStyle = UIModalPresentationFormSheet;
[self.viewController presentModalViewController:popup animated:YES];

有什么想法吗?


我已经用下面的方法解决了,在这里展示给未来的用户。

在情节提要上,选定的原型单元格在表格视图控制器(根视图控制器)中可用 - #1

单击并创建了一个样式为“Push”的“故事板转场”,并将其称为“详细信息”

在我的代码中,我在下面成功调用了 #2 (UIVwCtrl)

[self performSegueWithIdentifier:@"Detail" sender:self]; 

【问题讨论】:

  • 那么问题出在哪里?您不能以模态方式呈现 viewController 吗?

标签: ios objective-c uiviewcontroller


【解决方案1】:

如果你使用Storyboard,你应该使用instantiateViewControllerWithIdentifier 方法来加载视图控制器:

YourViewControllerClass *popup = [[UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil] instantiateViewControllerWithIdentifier:@"YourPopupViewControllerID"];
popup.modalPresentationStyle = UIModalPresentationFormSheet;

[self presentModalViewController:popup animated:YES];

注意方法:

+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil

采用故事板名称没有文件扩展名。

【讨论】:

  • 感谢 reecon。没运气。收到错误“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'在捆绑包NSBundle中找不到名为'Main.storyboard'的故事板”用于代码--- vcControl2 *popup = [[UIStoryboard storyboardWithName:@“Main.故事板“捆绑:nil]实例化ViewControllerWithIdentifier:@“Control2”];
  • 不提供@"Main.storyboard",而是使用@"Main" 作为故事板名称来调用方法。
猜你喜欢
  • 1970-01-01
  • 2016-09-20
  • 2017-08-06
  • 2022-08-13
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多