메서드에서 만든 모달보기가 있고 (메인 뷰에 참조가 없습니다) 내 앱이 백그라운드에서 입력 될 때 자동으로 dismissModalViewControllerAnimated를 수행하고 싶습니다. 어떻게해야합니까?응용 프로그램이 백그라운드에서 ios에 입력 할 때 모달보기를 닫는 방법
3
A
답변
9
메인 뷰의 viewDidLoad에서 앱이 백그라운드로 전환되면 알림을 받도록 추가합니다.
- (void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(goToBackground)
name:UIApplicationWillResignActiveNotification object:nil];
}
goToBackground() 함수를 정의하십시오. 응용 프로그램이 백그라운드로 전환 될 때 호출됩니다
- (void) goToBackground
{
[self dismissModalViewControllerAnimated: NO]; // no need to animate
}
관찰자
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
을 제거하는 것을 잊지 마세요
1
알림을 사용할 수 있습니다. ApplicationDelegate의 메소드 applicationDidEnterBackground에서 알림을 게시하십시오. 모달 컨트롤러에서 닫기 메서드를 호출 할 수 있으므로 옵저버로 알림 센터에 추가하십시오.