【问题标题】:IOS: Unable to access properties and methods on presentingViewController from presentedViewControllerIOS:无法从presentingViewController访问presentingViewController的属性和方法
【发布时间】:2012-03-21 00:02:45
【问题描述】:

我正在尝试创建一个嵌套的故事板结构来帮助分解一个巨大的故事板(SVN 合并噩梦)......无论如何,在这种情况下我有一个父故事板/viewController 'NestedStoryTestViewContoller' 和另外两个故事板/ViewControllers(StoryOne和 StoryTwo)

当您单击 NestedStoryTestViewContoller 中的按钮时,StoryOne 会作为模式视图启动。然后我可以关闭它并返回到 NestedStoryTestViewController。但是,如果我想直接从 StoryOne 转到 StoryTwo,我会遇到问题。我想做的是:

-(IBAction)goToStoryTwo{

    UIStoryboard *storyTwo = [UIStoryboard storyboardWithName:@"BoardTwo" bundle:nil];
    UIViewController *boardTwoVC = [storyTwo instantiateInitialViewController];
    boardTwoVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.presentingViewController presentModalViewController:boardTwoVC animated:YES];   

}

但这无济于事。

我可以通过以下方式关闭模态视图:

-(IBAction)goToMain{

     [self dismissModalViewControllerAnimated:YES];
 }

我可以在关闭 modalview 后从 NestedStoryTestViewController 的 viewDidLoad 方法调用一个新的 ModalView,但要让它工作,我需要在 NestedStoryTestViewCOntroller 上调用某种方法来设置一个属性,告诉它要加载哪个新的 modalview。

当我尝试调用这样的方法时,我得到了一个不错的大胖编译器错误,似乎没有任何原因:

[self.presentingViewController setViewToLoad:@"StoryOne"];

触发错误:自动引用计数问题 'Reciever type 'UIViewController' for instance message does not declare a method with selector 'setViewToLoad''

该方法已明确定义和实施,我不知道为什么会引发此错误。尝试在“presentingViewController”上设置任何属性时,我也会遇到同样的错误。如果我输入如下日志语句:

NSLog(@"parent class : %@",[[self presentingViewController] class] );

它按预期记录“父类:NestedStoryTestViewController”。

非常感谢任何帮助,因为这是为了工作,因此非常重要和及时。如有需要,我可以提供项目文件。

谢谢, 克里斯

请注意:我知道名称从 'parentViewController' 更改为 'presentingViewController' 并且项目仅为 5.0,所以这不是问题。

【问题讨论】:

  • 您的第二个问题可以通过使用[(NestedStoryTestViewController *)self.presentingViewController setViewToLoad:@"StoryOne"]; 来解决。但不确定第一个问题。
  • 由于这些是不相关的问题,您最好将它们作为单独的问题发布。
  • 甜!你是很棒的 Inafzger 先生!另一个问题实际上只是关于我为什么要以我的方式进行此操作的背景,我很确定它不起作用,因为在之前的 modalview 被动画化之前我无法创建另一个 modalview。但有了您的建议,我可以继续实施我的多故事板解决方案。
  • 不客气!由于您是 Stack Overflow (SO) 的新手,请确保您接受对您有帮助的答案,以便让其他人知道什么对您有用! (要接受,请单击相应答案旁边的复选标记)。这也给帮助过的人加分,是感谢他们的好方法

标签: objective-c ios xcode4 storyboard


【解决方案1】:

您在以下行中收到错误的原因:

[self.presentingViewController setViewToLoad:@"StoryOne"];

是因为您正在调用 NestedStoryTestViewController 类的方法,但编译器认为它是 UIViewController 类。

由于您知道类是什么,您需要将其“转换”(或强制转换)为正确的类型,以便编译器知道它将响应您的方法,如下所示:

[(NestedStoryTestViewController *)self.presentingViewController setViewToLoad:@"StoryOne"];

【讨论】:

  • 按照这个答案我做了'[(ItViewController *)self.presentingViewController setSelectedBook:[_BookItems objectAtIndex:[indexPath row]]];'。我也从 xcode 上的智能感知中获得“setSelectedBook”,但是在运行时出现以下错误,有什么想法吗? "[UINavigationController setSelectedBook:]: 无法识别的选择器发送到实例 0x6b4ad10'"
  • 我也是 Rohit,你找到解决方案了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2013-10-27
相关资源
最近更新 更多