![](/img/trans.png)
[英]iOS:How to dismiss other UIViewcontroller in current UIViewcontroller
[英]ios - How can I dismiss(close) the load UIViewController on the window
我有一個問題,我想構建像UIAlertView
這樣的彈出視圖,
我在情節UIViewController
中創建兩個UIViewController
(注意:沒有segue),並且根UIViewController
將加載UIViewController
視圖彈出窗口。
我創建了兩個UIViewController
來編輯UIView
,因為我想使用Storybaord編輯功能中的復雜UIView
。
然后我將容器視圖放在RootViewController
上,並且容器視圖中有兩個按鈕。(我的結構如下:)
當我單擊綠色的彈出按鈕(按鈕名稱為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];
}
當我點擊按鈕時,它可以糾正窗口上的彈出窗口:
但是現在,當我單擊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看到我這個簡單的項目)
非常感謝你。
-(void) tapAction:(UITapGestureRecognizer*) recognizer
{
thirdViewcontroller.hidden=YES;
}
我解決了這個問題。 但是我不知道有什么最好的答案。
我的方法在popupGreenBtnAction actin中。
將代碼更改為:
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
secondGreenVC.modalPresentationStyle = UIModalPresentationCustom;
[appDelegate.window.rootViewController presentViewController:secondGreenVC animated:NO completion:nil];
然后在SecondGreenViewController故事板上,
拖動另一個基於UIView的UIView並設置UIView IBOutlet(現在,我將其命名為“ 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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.