我正在使用以下代码添加子视图控制器:
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
仍然可见,但视图停止按预期反应。 UITextViews
上的MyCustomViewController
不会在MyCustomViewController.m
等中触发其委托方法。
对于Clarity:我仍然可以点击UITextFields
上的MyCustomViewController
,当我点按它们时仍然可以看到按钮的反应,但它们并未触发相关联IBActions
等等。
任何有关正在发生的事情的想法都将非常感激。
其他信息
我添加了一种关闭模态的方法,使用[self.navigationController dismissViewControllerAnimated:YES completion:nil];
中的MyModalViewController
并将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>
但是......在我解雇模态并重新运行po [[[[UIApplication sharedApplication] keyWindow] rootViewController] _printHierarchy]
后,<MyCustomViewController 0x7fc238d48e50>
丢失了。
问题可能源于<MyCustomViewController>
说&#34;出现&#34;并且没有说&#34;出现&#34; ?
我的解决方案 我从
更改了MyCustomViewController
UIViewController *parentViewController = self.tabBarController;
到
UIViewController *parentViewController = self.tabBarController.navigationController;
感谢所有帮助过的人。我会回顾你的答案,希望下次我能更好地完成这个效果。
答案 0 :(得分:1)
问题是由于您的MyCustomViewController
已从其父控制器中删除而引起的。那时你的活动就不复存在了。
从您发布的代码中我无法说出删除了控制器的内容但您应该可以使用-[MyCustomViewController removeFromParentViewController]:
中的断点轻松调试它。您要么是明确地删除了它,要么UITabBarController
将其自行删除(可能由下面描述的问题引起)。
然而,还有另一个大问题:
UITabBarController
是容器控制器。控制器的重点是管理其子视图控制器。这也意味着控制器正在管理哪个控制器正在接收外观回调(例如viewWillAppear:
,viewDidAppear:
等等)。通过添加另一个子视图控制器,您正在做一些不受支持的事情。
例如,当UITabBarController
出现时,它仅向其选定的控制器发送viewDidAppear:
。它不会将viewDidAppear:
发送给您的其他控制器。
这可能导致许多非常有问题的状态,我认为这是你问题的根源。
您可以考虑以下层次结构(为标签栏控制器和自定义控制器引入公共父级),而不是将子控制器直接添加到UITabBarController
。
UIViewController
| UITabBarController
| MyCustomViewController
答案 1 :(得分:0)
你需要确保你正在解雇模态中的navigationController
...否则它只是解雇内部控制器(不是它的父亲!)并且仍然存在(并拦截你的事件)
- (void)closeTheModal:(id)sender {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
答案 2 :(得分:0)
您的问题出在此代码中
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
。因此,与self一起发布的实例可能会MyCustomViewController
无效。
这样做,
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:**vc** action:@selector(closeTheModal:)];
vc.navigationItem.leftBarButtonItem = button;
并在MyModalViewController
类中声明选择器并检查。