Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 子视图控制器在显示和取消模式视图后丢失委托_Ios_Objective C_Delegates_Presentviewcontroller_Childviewcontroller - Fatal编程技术网

Ios 子视图控制器在显示和取消模式视图后丢失委托

Ios 子视图控制器在显示和取消模式视图后丢失委托,ios,objective-c,delegates,presentviewcontroller,childviewcontroller,Ios,Objective C,Delegates,Presentviewcontroller,Childviewcontroller,我正在使用以下代码添加子视图控制器: MyCustomViewController * overlayView = [[MyCustomViewController alloc] initWithNibName:@"MyCustom" bundle:nil]; UIViewController *parentViewController = self.tabBarController; [modalView willMoveToParentViewController:parentViewCo

我正在使用以下代码添加子视图控制器:

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
触发
关闭模式:

- (void)closeTheModal:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
模式关闭后,
MyCustomViewController
仍然可见,但视图会按预期停止反应。
MyCustomViewController
上的
uitextview
不会触发它们在
MyCustomViewController.m
等中的委托方法

为清晰起见:我仍然可以点击
MyCustomViewController
上的
UITextFields
,并且在点击按钮时仍然可以看到按钮的反应,但它们不会触发相关的
iActions
,等等

如果您对正在发生的事情有任何想法,我们将不胜感激

其他信息 我添加了一个关闭模式的方法,使用
[self.navigationController dismissViewControllerAnimated:YES completion:nil]MyModalViewController
中编写>并将
操作:@selector()
指向该方法

MyModalViewController
中的方法触发并关闭模式,但是
MyCustomViewController
仍然没有响应

po[[[UIApplication sharedApplication]keyWindow]rootViewController]\u printHierarchy] 当我运行
po[[[[UIApplication sharedApplication]keyWindow]rootViewController]\u 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>
,状态:已出现,视图:
|,状态:已出现,视图:
||,状态:已出现,视图:
|| |,状态:已出现,视图:
||,状态:消失,视图:(视图未加载)
|| |,状态:消失,视图:(视图未加载)
||,状态:消失,视图:(视图未加载)
|| |,状态:消失,视图:(视图未加载)
||,状态:出现,视图:
但是。。。在我关闭模式并重新运行
po[[[[UIApplication sharedApplication]keyWindow]rootViewController]\u printHierarchy]
后,
丢失

也许问题的根源在于
说的是“出现”,而不是“出现”

我的解决方案 我从更改了MyCustomViewController

UIViewController*parentViewController=self.tabBarController

UIViewController*parentViewController=self.tabBarController.navigationController


感谢所有帮助过我的人。我将回顾您的答案,希望下次我能更好地实现这一效果。

您需要确保在模式中解除导航控制器的功能。
。。。否则,它只是解除内部控制器(而不是它的父级!),并且仍然存在(并拦截您的事件)


此问题是由于您的
MyCustomViewController
已从其父控制器中删除。那么你的活动就停止了

从您发布的代码中,我无法说明是什么原因删除了控制器,但您应该能够使用
-[MyCustomViewController removeFromParentViewController]:
中的断点轻松调试控制器。
您可能是错误地显式删除了它,或者
UITabBarController
自己删除了它(可能由以下所述问题引起)

然而,还有一个大问题:

UITabBarController
是一个容器控制器。控制器的全部功能是管理其子视图控制器。这也意味着控制器正在管理接收外观回调的控制器(例如,
视图将出现:
视图显示:
等等)。通过添加另一个子视图控制器,您正在执行不受支持的操作

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

UIViewController
   |  UITabBarController
   |  MyCustomViewController
这可能会导致许多非常有问题的州,我认为这是你问题的根源

< >不要直接将子控制器添加到<代码> UITabBarController < /C>中,您可以考虑以下层次结构(为标签栏控制器和自定义控制器引入公共父级)。
你的问题在这段代码中

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
中声明它时,self将是一个
MyCustomViewController
实例。并且可能是您在同一类中声明了选择器
closemodel
。因此,与self一起发布的实例可能是
MyCustomViewController
insatnce

这样做,

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

然后在
MyModalViewController
类中声明选择器并进行检查。

这是否需要进入
MyModalViewController
?在
MyCustomViewController
中不起作用。是否可以从
MyCustomViewController
中执行它?我在
MyModalViewController
中添加了一个方法,并指出了
操作:@selector()
添加到该方法。
MyModalViewController
中的方法触发并关闭模式,但
MyCustomViewController
仍没有响应。如果没有正确添加控制器,则添加时不应手动调用
didMoveToParentViewController
。当您记录
所有目标时会发生什么n您的按钮?您的操作是否仍然正确地附加到按钮上?控制台中是否存在任何警告/错误?在取消(
[[UIWindow keyWindow]rootViewController]\u printHierarchy]
)后记录控制器层次结构时会发生什么情况?记录
[[u按钮所有目标]
在模式前后产生相同的结果:
。不是
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:**vc** action:@selector(closeTheModal:)];
vc.navigationItem.leftBarButtonItem = button;