티스토리 뷰
개발하다 보면 뷰컨트롤러가 아닌 AppDelegate나 NSObject 클래스에서 UIAlertController를 띄우고 싶을때가 종종 있다.
UIAlertController를 사용할려면 뷰컨트롤러에서 present를 해야하고 최상위가 아닌 뷰컨트롤러에서 시도할 경우 아래와 같이 계층 에러가 발생한다.
(참고: viewDidLoad에서 바로 다른 뷰컨트롤러를 present를 하게 될 경우도 발생한다. 화면이 다 보여지지 않은 상태에서 다른 뷰컨트롤러를 보여준다는게 말이 안되기 때문이다.)
Warning: Attempt to present <UIViewController: 0x84ac3b0> on <ViewController: 0x81b0230> whose view is not in the window hierarchy!
그래서 아래와 같이 최상위 뷰컨트롤러를 구하는 클래스 함수를 만들어서 사용하고 있다.
class func topViewController() -> UIViewController? {
if let keyWindow = UIApplication.shared.keyWindow {
if var viewController = keyWindow.rootViewController {
while viewController.presentedViewController != nil {
viewController = viewController.presentedViewController!
}
print("topViewController -> \(String(describing: viewController))")
return viewController
}
}
return nil
}
'iOS > UIApplication' 카테고리의 다른 글
iOS APNs 인증 키 p8 (iOS Key로 푸시 보내기) (2) | 2017.12.06 |
---|---|
iOS 앱 아이콘 동적 변경 (setAlternateIconName) (0) | 2017.07.31 |
- Total
- Today
- Yesterday
- httpCookieStore
- 이미지 푸시
- 인증 키
- UITextFieldTextDidChange
- Create a New Key
- shouldChangeCharactersIn
- wkwebview
- iOS Keys
- iOS10
- whose view is not in the window hierarchy
- swift
- mutable-content
- UNUserNotificationCenterDelegate
- swift3
- 앱 아이콘
- ios
- UNUserNotificationCenter
- NotificationCenter
- Notification Service Extension
- appDelegate
- Easy APNs Provider
- 리치 푸시
- WebSiteDataStore
- UITextFiled
- UIApplication
- setAlternateIconName
- AppIcon
- apns
- presentedViewController
- UITextFieldDelegate
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |