![](/img/trans.png)
[英]iOS:How to dismiss other UIViewcontroller in current UIViewcontroller
[英]ios - How can I dismiss(close) the load UIViewController on the window
我有一个问题,我想构建像UIAlertView
这样的弹出视图,
我在情节UIViewController
中创建两个UIViewController
(注意:没有segue),并且根UIViewController
将加载UIViewController
视图弹出窗口。
我创建了两个UIViewController
来编辑UIView
,因为我想使用Storybaord编辑功能中的复杂UIView
。
然后我将容器视图放在RootViewController
上,并且容器视图中有两个按钮。(我的结构如下:)
当我单击绿色的弹出按钮(按钮名称为popupGreenBtn
)时,它可以更正UIWindow
上的addSubview
。
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"ContainerViewController view didload");
appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
secondGreenVC = (SecondGreenViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"SecondGreenViewController"];
thirdRedVC = (ThirdRedViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"ThirdRedViewController"];
}
- (IBAction)popupGreenBtnAction:(id)sender {
[appDelegate.window addSubview: secondGreenVC.view];
[appDelegate.window bringSubviewToFront:secondGreenVC.view];
}
- (IBAction)popupRedBtnAction:(id)sender {
[appDelegate.window addSubview: thirdRedVC.view];
[appDelegate.window bringSubviewToFront:thirdRedVC.view];
}
当我点击按钮时,它可以纠正窗口上的弹出窗口:
但是现在,当我单击SecondGreenViewController
和ThirdRedViewcontroller
背景视图(只是透明的深黑色)时,我想关闭该弹出窗口( secondGreenVC.view
或thirdRedVC.view
)。
我曾尝试在下面的SecondGreenViewController
类中添加代码:
@implementation SecondGreenViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[_secondBaseView addGestureRecognizer:tap];
}
-(void) tapAction:(UITapGestureRecognizer*) recognizer
{
[self removeFromParentViewController];
// [self dismissViewControllerAnimated:NO completion:nil];
NSLog(@"tap in SecondGreenViewController");
}
@end
关闭UIViewController
无效。
单击深黑色部分时如何关闭弹出窗口( UIViewController
)?
(如果您想了解更多信息,请告诉我,或者您可以从github https://github.com/dickfalaDeveloper/PopUpViewDemo看到我这个简单的项目)
非常感谢你。
-(void) tapAction:(UITapGestureRecognizer*) recognizer
{
thirdViewcontroller.hidden=YES;
}
我解决了这个问题。 但是我不知道有什么最好的答案。
我的方法在popupGreenBtnAction actin中。
将代码更改为:
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
secondGreenVC.modalPresentationStyle = UIModalPresentationCustom;
[appDelegate.window.rootViewController presentViewController:secondGreenVC animated:NO completion:nil];
然后在SecondGreenViewController故事板上,
拖动另一个基于UIView的UIView并设置UIView IBOutlet(现在,我将其命名为“ secondBaseView”)。
在SecondGreenViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.secondBaseView addGestureRecognizer:tap];
}
-(void) tapAction:(UITapGestureRecognizer*) recognizer
{
[self dismissViewControllerAnimated:NO completion:nil];
}
可以显示模态并关闭模态UIViewController。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.