当从其父视图中删除第二个 ViewController 时,FirstViewController 不起作用

Posted

技术标签:

【中文标题】当从其父视图中删除第二个 ViewController 时,FirstViewController 不起作用【英文标题】:FirstViewController is not working when Second ViewController is removed from its superview 【发布时间】:2012-07-12 06:24:24 【问题描述】:

我正在使用 UIStoryBoard 来允许 FirstViewController 将第二个 ViewController 的视图添加为子视图。关于使用以下方法移除子视图

FirstViewController.m

  - (IBAction) btnMoveTo:(id)sender


 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
 vc.view.backgroundColor = [UIColor clearColor];
 self.modalPresentationStyle = UIModalPresentationCurrentContext;
 [self presentModalViewController:vc animated:NO];


SecondViewController.m

-(void)viewDidLoad
   self.view.opaque = YES;
   self.view.backgroundColor = [UIColor clearColor];


 - (IBAction) withDraw:(id)sender
     [self.view removeWithZoomOutAnimation:2 option:nil];
    

当我访问 withDraw 函数时,SecondViewController 的视图被移除并且 firstViewController 又回来了。但是,当我使用按钮访问 - (IBAction) btnMoveTo:(id)sender 函数时。它不起作用。什么都没有发生。任何建议或帮助将不胜感激。

【问题讨论】:

【参考方案1】:

您将视图与视图控制器混淆了。视图控制器是控制视图的对象,所以对于你写的第一行,

我正在使用 UIStoryBoard 来允许 FirstViewController 将第二个 ViewController 的视图添加为子视图。关于使用以下方法移除子视图

您不能将视图控制器添加为另一个视图控制器的子视图。您呈现一个管理自己的视图的视图控制器,并且您可以将 UIViews 添加为 UIViewController 的子视图。一个UIViewController 管理UIViews。

您发布的代码遇到的问题是,当您触发 withDraw: 函数时,您正在删除视图控制器的 view。不是视图控制器本身。您需要关闭 SecondViewController 才能再次访问 FirstViewController。

【讨论】:

其实@Siddharthan并没有写到添加视图控制器作为子视图,而是添加“第二个ViewController的视图作为子视图”。

以上是关于当从其父视图中删除第二个 ViewController 时,FirstViewController 不起作用的主要内容,如果未能解决你的问题,请参考以下文章