子视图控制器失去了展示和解聘模式的看法

问题描述:

我使用这个代码添加子视图控制器后委托:子视图控制器失去了展示和解聘模式的看法

MyCustomViewController * overlayView = [[MyCustomViewController alloc] initWithNibName:@"MyCustom" bundle:nil]; 

UIViewController *parentViewController = self.tabBarController; 
[modalView willMoveToParentViewController:parentViewController]; 

// set the frame for the overlayView 
overlayView.view.frame = parentViewController.view.frame; 

[parentViewController.view addSubview: overlayView.view]; 

[parentViewController.view needsUpdateConstraints]; 
[parentViewController.view layoutIfNeeded]; 

// Finish adding the overlayView as a Child View Controller 
[parentViewController addChildViewController: overlayView]; 
[overlayView didMoveToParentViewController:parentViewController]; 

然后MyCustomViewController内的按钮,水龙头触发模式以呈现:

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

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc]; 
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(closeTheModal:)]; 
vc.navigationItem.leftBarButtonItem = button; 

[self.navigationController presentViewController:navigationController animated:YES completion:nil]; 

轻敲UIBarButtonItem触发closeTheModal:

- (void)closeTheModal:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

模式关闭后,MyCustomViewController仍然可见,但视图停止按预期进行反应。在MyCustomViewControllerUITextViews不会触发他们的代表方法MyCustomViewController.m

为清楚起见:我仍然可以挖掘到UITextFieldsMyCustomViewController,仍然可以看到按钮,当我点击他们的反应,但他们都没有触发其相关IBActions

任何关于正在发生的想法将不胜感激。

附加信息 我添加了一个方法来关闭的模态,在使用MyModalViewController[self.navigationController dismissViewControllerAnimated:YES completion:nil];指向的action:@selector()给方法。

MyModalViewController中的方法触发并关闭模式,但MyCustomViewController仍未响应。

PO [[[[UIApplication的sharedApplication] keyWindow] RootViewController的] _printHierarchy] 当我运行po [[[[UIApplication sharedApplication] keyWindow] rootViewController] _printHierarchy],提出了模态视图之前,我看到:

<UINavigationController 0x7fc23985b200>, state: appeared, view: <UILayoutContainerView 0x7fc238f3b760> 
    | <MyTabBarController 0x7fc23a045400>, state: appeared, view: <UILayoutContainerView 0x7fc238cc7500> 
    | | <MyNavigationController 0x7fc23985a800>, state: appeared, view: <UILayoutContainerView 0x7fc23b0497e0> 
    | | | <FirstTabViewController 0x7fc238d47650>, state: appeared, view: <UIView 0x7fc23b1318a0> 
    | | <UINavigationController 0x7fc23a0bc000>, state: disappeared, view: (view not loaded) 
    | | | <SecondTabViewController 0x7fc238c8da90>, state: disappeared, view: (view not loaded) 
    | | <UINavigationController 0x7fc23987c800>, state: disappeared, view: (view not loaded) 
    | | | <ThirdTabViewController 0x7fc238f77c00>, state: disappeared, view: (view not loaded) 
    | | <MyCustomViewController 0x7fc238d48e50>, state: appearing, view: <UIView 0x7fc23b086220> 

但是......我后解除Modal并重新运行po [[[[UIApplication sharedApplication] keyWindow] rootViewController] _printHierarchy]<MyCustomViewController 0x7fc238d48e50>丢失。

也许问题源于这样的事实,即<MyCustomViewController>说“出现”,并没有说“出现”?

我的解决方案 我改变MyCustomViewController

UIViewController *parentViewController = self.tabBarController;

UIViewController *parentViewController = self.tabBarController.navigationController;

感谢大家,帮助。我会回顾你的答案,希望下次能更好地完成这个效果。

+1

您没有正确添加控制器,添加时不应该手动调用'didMoveToParentViewController'。 – Sulthan

+0

当您在按钮上登录allTargets时会发生什么?你的行为是否仍然正确地连接到按钮?控制台中是否有任何警告/错误?在解散之后记录控制器层次结构时会发生什么('[[[UIWindow keyWindow] rootViewController] _printHierarchy]')? – Sulthan

+0

记录'[_theButton allTargets]'在Modal之前和之后产生相同的结果:''。 不确定'_printHierarchy',试图找出如何使用它。 – Chris

的问题是由事实,你MyCustomViewController接到其父控制器中删除造成的。那么你的活动就会停止。

从你发布的代码我不能说是什么让控制器被删除,但你应该能够很容易地调试它使用-[MyCustomViewController removeFromParentViewController]:断点你可能是明确地删除它或UITabBarController本身(可能是由下面描述的问题引起)。

然而,还有另外一个大问题:

UITabBarController是一个容器控制器。控制器的全部重点是管理其子视图控制器。这也意味着控制器正在管理哪个控制器正在接收外观回调(例如,viewWillAppear:,viewDidAppear:等)。通过添加另一个子视图控制器,你正在做一些不被支持的东西。

例如,当UITabBarController出现时,它仅将viewDidAppear:发送到其选定的控制器。它不会将viewDidAppear:发送到您的其他控制器。

这可能会导致许多非常有问题的状态,我认为这是问题的根源。

除了将子控制器直接添加到UITabBarController之外,您可以考虑以下层次结构(为选项卡栏控制器和自定义控制器引入一个公共父级)。

UIViewController 
    | UITabBarController 
    | MyCustomViewController 

你需要确保你正在解除模态内的navigationController ...否则它只是解雇内部控制器(不是它的父!),并且仍然存在(和你的拦截事件)

- (void)closeTheModal:(id)sender { 
    [self.navigationController dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

这需要进入'MyModalViewController'吗?不能从'MyCustomViewController'工作。任何方式从'MyCustomViewController'执行它? – Chris

+0

我在'MyModalViewController'中添加了一个方法,并将'action:@selector()'指向该方法。 'MyModalViewController'中的方法触发并关闭模式,但是'MyCustomViewController'仍然没有响应。 – Chris

您的问题是在此代码

MyModalViewController *vc = [[MyModalViewController alloc] initWithNibName:@"DirectEmployerSignup" bundle:nil]; 
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc]; 
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(closeTheModal:)]; 
vc.navigationItem.leftBarButtonItem = button; 
[self.navigationController presentViewController:navigationController animated:YES completion:nil]; 

当你声明,

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(closeTheModal:)]; 
vc.navigationItem.leftBarButtonItem = button; 

定下的目标是self,因为你是在MyCustomViewController宣布它,自会一个MyCustomViewController实例。可能你已经在同一个类中声明了选择器closeTheModal。因此,自我释放的实例可能是MyCustomViewController insatnce。

做到这一点,

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:**vc** action:@selector(closeTheModal:)]; 
vc.navigationItem.leftBarButtonItem = button; 

而且在MyModalViewController类中声明的选择和检查。