我有一个问题,我想构建像UIAlertView
,
我在故事板中创建了两个UIViewController
(注意:没有segue),Root UIViewController
将加载UIViewController
视图弹出窗口。
我创建了两个UIViewController
来修改UIView
,因为我想使用storybaord编辑功能中的复杂UIView
。
然后我将容器视图放在RootViewController
上,容器视图中有两个按钮。(我的结构如下:)
当我点击弹出式绿色按钮(按钮名称为popupGreenBtn
)时,它可以更正addSubview
上的UIWindow
。
- (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看到我这个简单的项目)
非常感谢。
答案 0 :(得分:0)
-(void) tapAction:(UITapGestureRecognizer*) recognizer
{
thirdViewcontroller.hidden=YES;
}
答案 1 :(得分:0)
我解决了这个问题。但我不知道有什么最好的答案。
我的方法是在popupGreenBtnAction肌动蛋白中。
将代码更改为:
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
secondGreenVC.modalPresentationStyle = UIModalPresentationCustom;
[appDelegate.window.rootViewController presentViewController:secondGreenVC animated:NO completion:nil];
然后在SecondGreenViewController故事板中,
拖动另一个UIView do base UIView并设置UIView IBOutlet(现在我用&#34命名; 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。