ios - 如何解除(关闭)窗口上的加载UIViewController

时间:2015-03-09 03:33:23

标签: ios uiviewcontroller popup containers dismiss

我有一个问题,我想构建像UIAlertView

这样的弹出式视图

我在故事板中创建了两个UIViewController(注意:没有segue),Root UIViewController将加载UIViewController视图弹出窗口。

我创建了两个UIViewController来修改UIView,因为我想使用storybaord编辑功能中的复杂UIView

然后我将容器视图放在RootViewController上,容器视图中有两个按钮。(我的结构如下:)

enter image description here

当我点击弹出式绿色按钮(按钮名称为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];

 }

当我点击按钮时,它可以更正窗口上的弹出窗口:

enter image description here enter image description here

但现在,当我点击SecondGreenViewControllerThirdRedViewcontroller背景视图(只是透明的深黑色)时,我想要关闭视图(secondGreenVC.viewthirdRedVC.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看到我这个简单的项目)

非常感谢。

2 个答案:

答案 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。