programing

ViewController가 Modal로 표시되는지 여부를 확인할 수 있습니까?

subpage 2023. 6. 3. 08:27
반응형

ViewController가 Modal로 표시되는지 여부를 확인할 수 있습니까?

ViewController 클래스 내부에서 Modal ViewController로 표시되는지 확인할 수 있습니까?

때부터modalViewController에서는 더 이상 되지 않습니다. . iOS 6에서는 를 사용할 수 없습니다. 여기 iOS 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
}

펠리페의 대답에 대한 모자 팁.

iOS 6+를 찾는다면 이 답변은 사용하지 않으며 가브리엘 페트로넬라의 답변을 확인해야 합니다.


UIKit에 고유한 속성 또는 메서드로서 이를 수행할 수 있는 깔끔한 방법은 없습니다.컨트롤러가 모달로 표시되는지 확인하기 위해 컨트롤러의 여러 측면을 확인할 수 있습니다.

따라서 전류(표시값:self 컨트롤러가 저는 있습니다. 아 코 드 에 가 방 모 표 기 아 있 가 능 의 니 을 습 다 고 지 래 나 는 래 서 이 관 없 으 는 되 계 여 시 컨 러 롤UIViewControllercategory를 사용할 필요가 없는 에는 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: 추가, 서 EDIT 2: iOS 5+ 체크 추가,UIViewController 지지않음임에 대해 .parentViewController 이상, 더이상에게, 하만지.presentingViewController대신.

EDIT 3: 만약을 위해 https://gist.github.com/3174081 에 대한 요지를 작성했습니다.

iOS5+에서는 UIViewController Class Reference에서 볼 수 있듯이 "presentingViewController" 속성에서 가져올 수 있습니다.

presentingViewController 이 뷰 컨트롤러를 표시한 뷰 컨트롤러입니다.(읽기 전용)

읽기 @property(속성, 읽기 전용) UIViewController *표시ViewController
토론

이 메시지를 받은 보기 컨트롤러가 다른 보기 컨트롤러에 의해 표시되는 경우 이 속성은 이 메시지를 표시하는 보기 컨트롤러를 보유합니다.뷰 컨트롤러가 표시되지 않고 해당 상위 항목 중 하나가 표시되는 경우 이 속성은 가장 가까운 상위 항목을 표시하는 뷰 컨트롤러를 보유합니다.뷰 컨트롤러와 해당 상위 항목이 모두 제공되지 않는 경우 이 속성은 0으로 유지됩니다.


iOS 5.0 이상에서 사용할 수 있습니다.
된 In 언된위치
UIView 트롤러.h

할 수 .presentedAsModal) UIViewController 하위 클래스에서 다음과 같이 설정합니다.YES보기 컨트롤러를 모달 보기로 표시하기 전에 표시합니다.

childVC.presentedAsModal = YES;
[parentVC presentModalViewController:childVC animated:YES];

이 값은 에서 확인할 수 있습니다.viewWillAppear오버라이드

저는 견해가 어떻게 제시되는지를 명시하는 공식적인 속성은 없다고 생각하지만, 어떤 것도 여러분이 자신의 견해를 창조하는 것을 방해하지는 않습니다.

self.navigationController가 모듈식으로 표시되지만 self가 self.navigationController.viewController[0]와 같지 않은 경우에는 Petronella의 대답이 작동하지 않습니다. 이 경우 self.navigationController.viewController[0].

문제를 해결할 수 있는 방법은 다음과 같습니다.

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");
    }

전체 화면 모달 뷰와 비모달 뷰를 구분할 필요가 없다면(폼 시트와 페이지 시트에서만 발생하는 문제를 처리하고 있었습니다), UIViewController의 모달 PresentationStyle 속성을 사용할 수 있습니다.

switch (self.modalPresentationStyle) {
    case 0: NSLog(@"full screen, or not modal"); break;
    case 1: NSLog(@"page sheet"); break;
    case 2: NSLog(@"form sheet"); break;
}

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
}

내 프로젝트에는 모형으로(새 항목을 추가할 때) 또는 마스터 보기 컨트롤러에서 푸시로(기존 항목을 편집할 때) 표시할 수 있는 보기 컨트롤러(세부 정보)가 있습니다.사용자가 [완료]를 누르면 세부 보기 컨트롤러가 마스터 보기 컨트롤러의 메서드를 호출하여 닫을 준비가 되었음을 알립니다.마스터는 세부 정보를 닫는 방법을 알기 위해 세부 정보가 표시되는 방식을 결정해야 합니다.이렇게 해야 합니다.

UIViewController *vc = self.navigationController.viewControllers.lastObject;
if (vc == self) {
    [self dismissViewControllerAnimated:YES completion:NULL];
} else {
    [self.navigationController popViewControllerAnimated:YES];
}

이런 해킹은 효과가 있을지도 모릅니다.

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
}

하지만, 저의 이전 답변은 더 깨끗한 해결책이라고 생각합니다.

저에게 도움이 된 것은 다음과 같습니다.

// 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);

제가 테스트한 바로는 iOS7과 iOS8에서 작동합니다.하지만 iOS6는 시도하지 않았습니다.

저는 이 질문에 대한 올바른 답을 찾기 위해 주변을 좀 둘러보았지만, 가능한 모든 시나리오를 다루는 어떤 것도 찾을 수 없었습니다.저는 그 일을 하는 것처럼 보이는 이 몇 줄의 코드를 썼습니다.확인한 내용을 파악할 수 있는 인라인 의견을 몇 개 찾을 수 있습니다.

- (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;
}

도움이 되길 바랍니다.

다음은 @GabrielePetronella의 수정된 버전입니다. 이 버전은 상위 ViewController 계층을 먼저 이동한다는 점에서 포함된 ViewController와 함께 작동합니다.또한 코드를 여러 줄로 뽑아내어 무엇을 하는지 명확하게 했습니다.

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
}

언급URL : https://stackoverflow.com/questions/2798653/is-it-possible-to-determine-whether-viewcontroller-is-presented-as-modal

반응형