0

I have a question, I want to build popup view like the UIAlertView,

I create two UIViewController in the storyboard(note: there are not segue),and Root UIViewController will load the UIViewController view popup.

I create two UIViewController to edit the UIView, because I want to use the storybaord to edit my complex UIView in the feature.

Then I put the container view on the RootViewController and there are two button in the container view.(my structure as below:)

enter image description here

When I click the pop up green button(with button name popupGreenBtn), It can correct addSubview on the 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];

 }

When I click the button , it can correct popup on the window:

enter image description here enter image description here

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).

I had try to add the code in the SecondGreenViewController class below:

 @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.

How can I close the Popup window(the UIViewController) when I click the dark black part?

(If you want to more info, please tell me or you can see my this simple project from github https://github.com/dickfalaDeveloper/PopUpViewDemo )

Thank you very much.

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
dickfala
  • 3,246
  • 3
  • 31
  • 52

2 Answers2

0
  -(void) tapAction:(UITapGestureRecognizer*) recognizer
{
thirdViewcontroller.hidden=YES;
 }
Robin
  • 87
  • 7
  • Please avoid code only answers, add some explaination. – codingenious Mar 09 '15 at 06:01
  • you can dissmiss the thirdViewController if it is Presented over your secondViewcontroller.. here you added your thirdViewcontroller as a subview of secondViewcontroller... – Robin Mar 09 '15 at 07:18
0

I resolve the problem. But I don't know have any best answer.

My method is in the 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,

Drag an other UIView do base UIView and set the UIView IBOutlet(now I name with"secondBaseView").

at 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.

dickfala
  • 3,246
  • 3
  • 31
  • 52