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