[英]How to dismiss modal view controller if there are multiple view controller are presented
我有一個具有表視圖的視圖控制器A,並且基於行選擇,我將以模態方式打開另一個視圖控制器B。 我還為我的應用程序實現了不活動計時器。 現在,當B出現並且A在呈現控制器時,由於用戶的不活動,另一個視圖控制器“不活動”視圖控制器C將以模態方式在B之上打開。這意味着我擁有A,然后B處於A之上,C處於頂部B.現在,用戶單擊了C中的一個按鈕以注銷,而我只能關閉C。但是從未關閉View ControllerB。
不活動是使用觸摸事件和通知實現的,並且通知在當前視圖的頂部以模態形式顯示不活動視圖控制器,如以下代碼中所述。
- (void) applicationDidTimeout:(NSNotification *) notif
{
NSLog(@"Application Did Timeout ...");
BCDSessionInactivityViewController *sessionView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"InactivityViewController"];
sessionView.modalPresentationStyle = UIModalPresentationFormSheet;
sessionView.preferredContentSize = CGSizeMake(838,340);
UIViewController *parentController = self.parentViewController;
NSLog(@"presenting view controller is %@", [parentController class]);
[[self topViewController]presentViewController:sessionView animated:YES completion:nil];
}
- (UIViewController *)topViewController{
return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}
if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self topViewController:lastViewController];
}
UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self topViewController:presentedViewController];
}
有什么辦法我也可以解雇控制器B嗎?
在關閉視圖控制器C的View Controller C方法中,我根據建議編輯了代碼,但是View Controller B仍然沒有被關閉。
- (IBAction)logoutbtn:(id)sender
{
NSLog(@"logout is called");
if ([self.presentingViewController isKindOfClass:[BCDScanReviewViewController class]] || [[[F7CheckScanner instance]checkFrontScanned] isEqualToString:@"checkFrontScanned"])
{
NSLog(@"check front scanned %@",[[F7CheckScanner instance]checkFrontScanned]);
[[F7CheckScanner instance]scanBackOfCheckNoData];
}
[sessionTimer invalidate];
sessionTimer = nil;
[[BCDTimeManager sharedTimerInstance]stopIdleTimer];
[self dismissViewControllerAnimated:YES completion:^(){
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^(){;
BCDThankYouViewController *thankuView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ThankyouView"];
[[self topViewController ]presentViewController:thankuView animated:YES completion:nil];
}];
}];
UIViewController *parentController = self.presentingViewController;
NSLog(@"presenting view controller is %@", [parentController class]);
// [self dismissViewControllerAnimated:YES completion:^() {
// [parentController performSegueWithIdentifier:COMMON_VC_TO_THANK_YOU_VC sender:self];
// }];
}
你可以用C
做類似的事情,
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
要么
[self dismissViewControllerAnimated:YES completion:^{ /* do something when the animation is completed */ }];
[self.parentViewController dismissViewControllerAnimated:YES completion:^{ /* do something when the animation is completed */ }];
根據評論更新:
更換您的
[self dismissViewControllerAnimated:YES completion:^(){
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^(){;
BCDThankYouViewController *thankuView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ThankyouView"];
[[self topViewController ]presentViewController:thankuView animated:YES completion:nil];
}];
}];
與
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//you can do something here in completion instead of nil.
如果您要self.presentingViewController.presentingViewController.presentingViewController
三個viewcontroller,則可以使用self.presentingViewController.presentingViewController.presentingViewController
。
希望這會有所幫助:)
像這樣解散
//remove
[self removeFromParentViewController];
[self didMoveToParentViewController:nil];
[self.view removeFromSuperview];
感謝大家的建議。 我在其他SO posts的幫助下解決了這個問題。
-(void)dismissModalStack {
UIViewController *vc = self.presentingViewController;
while (vc.presentingViewController) {
vc = vc.presentingViewController;
}
NSLog(@"Dismissing the view controller at root of view hiearchy: %@", [vc class]);
[vc dismissViewControllerAnimated:YES completion:^() {
BCDThankYouViewController *thankuView=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ThankyouView"];
[[self topViewController ]presentViewController:thankuView animated:YES completion:nil];
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.