简体   繁体   English

ios-如何关闭窗口上的加载UIViewController

[英]ios - How can I dismiss(close) the load UIViewController on the window

I have a question, I want to build popup view like the UIAlertView , 我有一个问题,我想构建像UIAlertView这样的弹出视图,

I create two UIViewController in the storyboard(note: there are not segue),and Root UIViewController will load the UIViewController view popup. 我在情节UIViewController中创建两个UIViewController (注意:没有segue),并且根UIViewController将加载UIViewController视图弹出窗口。

I create two UIViewController to edit the UIView , because I want to use the storybaord to edit my complex UIView in the feature. 我创建了两个UIViewController来编辑UIView ,因为我想使用Storybaord编辑功能中的复杂UIView

Then I put the container view on the RootViewController and there are two button in the container view.(my structure as below:) 然后我将容器视图放在RootViewController上,并且容器视图中有两个按钮。(我的结构如下:)

在此处输入图片说明

When I click the pop up green button(with button name popupGreenBtn ), It can correct addSubview on the UIWindow . 当我单击绿色的弹出按钮(按钮名称为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];

 }

When I click the button , it can correct popup on the window: 当我点击按钮时,它可以纠正窗口上的弹出窗口:

在此处输入图片说明在此处输入图片说明

But now , When I click the SecondGreenViewController and ThirdRedViewcontroller background view(just transparent dark black), I want to close the popon the view( secondGreenVC.view or thirdRedVC.view ). 但是现在,当我单击SecondGreenViewControllerThirdRedViewcontroller背景视图(只是透明的深黑色)时,我想关闭该弹出窗口( secondGreenVC.viewthirdRedVC.view )。

I had try to add the code in the SecondGreenViewController class below: 我曾尝试在下面的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

There are not effect to close the UIViewController . 关闭UIViewController无效。

How can I close the Popup window(the UIViewController ) when I click the dark black part? 单击深黑色部分时如何关闭弹出窗口( UIViewController )?

(If you want to more info, please tell me or you can see my this simple project from github https://github.com/dickfalaDeveloper/PopUpViewDemo ) (如果您想了解更多信息,请告诉我,或者您可以从github https://github.com/dickfalaDeveloper/PopUpViewDemo看到我这个简单的项目)

Thank you very much. 非常感谢你。

  -(void) tapAction:(UITapGestureRecognizer*) recognizer
{
thirdViewcontroller.hidden=YES;
 }

I resolve the problem. 我解决了这个问题。 But I don't know have any best answer. 但是我不知道有什么最好的答案。

My method is in the popupGreenBtnAction actin. 我的方法在popupGreenBtnAction actin中。

change the code to: 将代码更改为:

  appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
  secondGreenVC.modalPresentationStyle = UIModalPresentationCustom;
  [appDelegate.window.rootViewController presentViewController:secondGreenVC animated:NO completion:nil];

Then in the SecondGreenViewController storyboard, 然后在SecondGreenViewController故事板上,

Drag an other UIView do base UIView and set the UIView IBOutlet(now I name with"secondBaseView"). 拖动另一个基于UIView的UIView并设置UIView IBOutlet(现在,我将其命名为“ secondBaseView”)。

at SecondGreenViewController.m 在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];
 }

That is Ok to show the modal and dismiss the modal UIViewController. 可以显示模态并关闭模态UIViewController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM