2011-03-17 3 views
49

모달 뷰가 있는지 확인하는 방법이 있습니까? 모달 뷰가있는 경우에만 메서드를 실행하고 싶습니다. 또한 여러 모달보기가있는 경우 특정 모달보기가 있는지 확인하는 방법이 있습니다.iOS - 모달 뷰가 있는지 확인하는 방법

나는 모달 뷰를 제시하고 닫으려면 다음 코드를 사용

[self presentModalViewController:myModalView animated:YES]; 
    [self dismissModalViewControllerAnimated:YES]; 

사전에 감사합니다!

건배 에반

PS. 내 모달보기에는보기 컨트롤러가 있지만 비동기 적으로 실행되는 별도의 클래스에서 모달보기가 있는지 확인하고 싶습니다.

+7

이 질문에 대한 정답을 변경하는 것을 고려하십시오 부모로부터 모달 view controller의 존재를 확인할 수 있습니다. – Daniel

답변

71

부모보기 컨트롤러에서 모달보기 컨트롤러가 있는지 확인하고 있습니까? 그렇다면하면, 당신은 단지 해당 뷰 컨트롤러의 modalViewController 속성을 확인할 수 있습니다

BOOL modalPresent = (self.modalViewController); 

특정 모달 뷰 컨트롤러를 확인하려면 다음과 같이 모달 뷰 컨트롤러의 클래스 이름을 얻을 수 있습니다 :

NSString *modalClassName = NSStringFromClass([self.modalViewController class]); 
+54

self.modalViewController는 이제 더 이상 사용되지 않으므로 BOOL modalPresent = (BOOL) (self.presentedViewController); – allaire

+2

@ allaire의 덧글에 피기 백하려면 여기 제공된 renderedViewController (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp)의 설명서를 참조하십시오./UIViewController/displayedViewController) 모달보기 컨트롤러를 제안하는 것 같다 "이"보기에 의해 제시되는 경우에만 존재합니다. – Danny

53

다음을 사용하여 확인할 수 있습니다 self.presentedViewController, 나를 위해 일한 무엇

+4

확인 된 답변이어야합니다. 'modalViewController'는 더 이상 사용되지 않습니다 – nburk

+1

이제'displayedViewController'를 사용하십시오. – WMios

5

The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy.이 따르고 반환 :

// 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에서 시도하지 않았습니다.

+0

UIViewController가 포함되어있는 경우 UINavigationController를 창의 rootViewController로 추가하는 것을 잊지 마십시오. 그런 다음 해당하는 navController에 대해 presentViewController를 호출하면됩니다. :) –

0

당신은 view controller

if ([[self presentingViewController] presentingViewController]) { 

} 
관련 문제