ViewController가 모달로 표시되는지 확인할 수 있습니까?
ViewController 클래스에서 모달보기 컨트롤러로 표시되는지 확인할 수 있습니까?
이후 modalViewController
아이폰 OS 6에서 사용되지 않으며, 여기에 버전 사용자들은 아이폰 OS 5+에 대한 작품과 경고없이 그 컴파일.
목표 -C :
- (BOOL)isModal {
return self.presentingViewController.presentedViewController == self
|| (self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController)
|| [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
}
빠른:
var isModal: Bool {
return self.presentingViewController?.presentedViewController == self
|| (self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController)
|| self.tabBarController?.presentingViewController is UITabBarController
}
Felipe의 답변에 대한 모자 설명.
iOS 6 이상을 찾고 있다면이 답변은 더 이상 사용되지 않으며 Gabriele Petronella의 답변을 확인해야합니다
UIKit 고유의 속성 또는 메서드로 깔끔한 방법이 없습니다. 컨트롤러의 여러 측면을 확인하여 모달로 표시되는지 확인하십시오.
따라서 현재 ( self
코드 벨로우즈로 표시) 컨트롤러가 모달 방식으로 표시 되는지 여부를 확인하려면 UIViewController
범주에 기능이 있거나 (프로젝트에서 다른 UIKit 컨트롤러를 사용할 필요가없는 경우) UITableViewController
예를 들어) 다른 컨트롤러가 상속하는 기본 컨트롤러에서
-(BOOL)isModal {
BOOL isModal = ((self.parentViewController && self.parentViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
( self.navigationController && self.navigationController.parentViewController && self.navigationController.parentViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] parentViewController] isKindOfClass:[UITabBarController class]]);
//iOS 5+
if (!isModal && [self respondsToSelector:@selector(presentingViewController)]) {
isModal = ((self.presentingViewController && self.presentingViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
(self.navigationController && self.navigationController.presentingViewController && self.navigationController.presentingViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]);
}
return isModal;
}
편집 : UITabBarController가 사용 중인지 확인하기 위해 마지막 검사를 추가했으며 다른 UITabBarController를 모달로 표시합니다.
편집 2 : 더 이상 UIViewController
대답하지 않고 대신 iOS 5 이상 검사를 추가 했습니다.parentViewController
presentingViewController
편집 3 : https://gist.github.com/3174081의 경우를 위해 요점을 만들었습니다.
iOS5 +에서는 UIViewController 클래스 참조 에서 볼 수 있듯이 "presentingViewController"속성에서 가져올 수 있습니다.
presentingViewController이 뷰 컨트롤러를 제공 한 뷰 컨트롤러입니다. (읽기 전용)
@property (비 원자, 읽기 전용) UIViewController * presentingViewController
토론
이 메시지를 수신 한 뷰 컨트롤러가 다른 뷰 컨트롤러에 의해 제공되는 경우이 속성은이를 표시하는 뷰 컨트롤러를 보유합니다. 뷰 컨트롤러가 표시되지 않지만 조상 중 하나가 표시되는 경우이 속성은 가장 가까운 조상을 나타내는 뷰 컨트롤러를 보유합니다. 뷰 컨트롤러와 조상이 표시되지 않으면이 속성은 nil을 유지합니다.
가용성
아이폰 OS 5.0 이상에서 사용할 수 있습니다. UIViewController.h에
선언
없는 경우 presentedAsModal
UIViewController 서브 클래스 에서이 속성 ( ) 을 정의 YES
하고 ViewController를 모달 뷰로 표시 하기 전에로 설정할 수 있습니다.
childVC.presentedAsModal = YES;
[parentVC presentModalViewController:childVC animated:YES];
viewWillAppear
재정의 에서이 값을 확인할 수 있습니다.
나는 그 견해가 어떻게 표현되는지를 나타내는 공식적인 재산이 없다고 생각하지만, 당신이 자신의 것을 창조하는 것을 방해하는 것은 없습니다.
self.navigationController가 모달로 표시되었지만 self가 self.navigationController.viewControllers [0]과 같지 않으면 Petronella의 답변 이 작동하지 않습니다.이 경우 self가 푸시됩니다.
다음은 문제를 해결하는 방법입니다.
return self.presentingViewController.presentedViewController == self
|| (self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController && self == self.navigationController.viewControllers[0])
|| [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
그리고 스위프트에서 :
return self.presentingViewController?.presentedViewController == self
|| (self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController && self.navigationController?.viewControllers[0] == self)
|| self.tabBarController?.presentingViewController is UITabBarController
이 작동합니다.
if(self.parentViewController.modalViewController == self)…
확인하는 가장 좋은 방법
if (self.navigationController.presentingViewController) {
NSLog(@"Model Present");
}
If you don't need to distinguish between full-screen modal views and non-modal views, which is the case in my project (I was dealing with a problem that only occurs with form sheets and page sheets), you can use the modalPresentationStyle property of UIViewController:
switch (self.modalPresentationStyle) {
case 0: NSLog(@"full screen, or not modal"); break;
case 1: NSLog(@"page sheet"); break;
case 2: NSLog(@"form sheet"); break;
}
In Swift:
func isUIViewControllerPresentedAsModal() -> Bool {
if((self.presentingViewController) != nil) {
return true
}
if(self.presentingViewController?.presentedViewController == self) {
return true
}
if(self.navigationController?.presentingViewController?.presentedViewController == self.navigationController) {
return true
}
if((self.tabBarController?.presentingViewController?.isKindOfClass(UITabBarController)) != nil) {
return true
}
return false
}
In my project I have a view controller (Detail) that can be presented either modally (when adding a new item) or with push (when editing an existing one) by Master view controller. When user taps [Done] the Detail view controller calls Master view controller's method to notify that it is ready to be closed. Master has to determine how Detail is presented in order to know how to close it. This is how I do this:
UIViewController *vc = self.navigationController.viewControllers.lastObject;
if (vc == self) {
[self dismissViewControllerAnimated:YES completion:NULL];
} else {
[self.navigationController popViewControllerAnimated:YES];
}
A hack like this might work.
UIViewController* child = self;
UIViewController* parent = child.parentViewController;
while (parent && parent.modalViewController != child) {
child = parent;
parent = child.parentViewController;
}
if (parent) {
// A view controller in the hierarchy was presented as a modal view controller
}
However, I think my previous answer is a cleaner solution.
What worked for me is following:
// this is the trick: set parent view controller as application's window root view controller
UIApplication.sharedApplication.delegate.window.rootViewController = viewController;
// assert no modal view is presented
XCTAssertNil(viewController.presentedViewController);
// simulate button tap which shows modal view controller
[viewController.deleteButton sendActionsForControlEvents:UIControlEventTouchUpInside];
// assert that modal view controller is presented
XCTAssertEqualObjects(viewController.presentedViewController.class, MyModalViewController.class);
As far as I tested it, this works for iOS7 and iOS8. Didn't try on iOS6 however.
I've looked a bit around to find the right answer to this question, and I couldn't find any which covered all the possible scenarios. I wrote these few lines of code which seem to do the job. You can find few inline comments to figure out what's been checked.
- (BOOL)isModal {
BOOL modal = NO;
if ([self presentingViewController]) { //Some view Controller is presenting the current stack
UIViewController *presented = [[self presentingViewController] presentedViewController]; // What's been presented
if ([presented respondsToSelector:@selector(viewControllers)]) { // There's a stack
NSArray *viewControllers = [presented performSelector:@selector(viewControllers)];
modal = [viewControllers firstObject] == self; // Current VC is presented modally if it's the first in the stack
}
else {
modal = presented == self; // Don't think this is actually needed. set modal = YES should do the job tho.
}
}
return modal;
}
Hope this help.
Here's my modified version of @GabrielePetronella's isModal
, which works with contained view controllers in that it walks up the parentViewController hierarchy first. Also pulled the code out into multiple lines so it's clear what it's doing.
var isModal: Bool {
// If we are a child view controller, we need to check our parent's presentation
// rather than our own. So walk up the chain until we don't see any parentViewControllers
var potentiallyPresentedViewController : UIViewController = self
while (potentiallyPresentedViewController.parentViewController != nil) {
potentiallyPresentedViewController = potentiallyPresentedViewController.parentViewController!
}
if self.presentingViewController?.presentedViewController == potentiallyPresentedViewController {
return true
}
if let navigationController = potentiallyPresentedViewController.navigationController {
if navigationController.presentingViewController?.presentedViewController == navigationController {
return true
}
}
return potentiallyPresentedViewController.tabBarController?.presentingViewController is UITabBarController
}
'Programing' 카테고리의 다른 글
IPython Notebook Server 3에서 함수 인수를 어떻게 볼 수 있습니까? (0) | 2020.07.21 |
---|---|
부울 크기가 1 비트가 아닌 1 바이트 인 이유는 무엇입니까? (0) | 2020.07.21 |
Matplotlib에 두 번째 (새) 플롯을 만든 다음 나중에 이전 플롯을 만들도록 어떻게 지시합니까? (0) | 2020.07.21 |
파이썬에서 stdout을 "nothing"으로 리디렉션 (0) | 2020.07.21 |
2 ~ 3 초 동안 지연을 추가하는 방법 (0) | 2020.07.21 |