2017-12-26 13 views
0

AppDelegate에서 특정보기 컨트롤러를 인스턴스화하는 것과 관련된 빠른 질문이 있습니다. 사용자가 원격 알림을 통해 앱을 열 때 응답해야하는 간단한 채팅 앱을 제작 중입니다. 내 앱의 바람직한 동작은 사용자가이 알림을 탭하면 애플리케이션이 특정 채팅 화면으로 열리는 것입니다. 다음UITabBarController 내부에서 현재 UINavigationController

앱의 기본 흐름은 다음

  • 최상위 UI 성분이 탭 바 안쪽 UITabBarController가
  • 이다되는 탐색 제어부 내부 탐색 컨트롤러
  • 되는 뷰 컨트롤러 사용자가 새 뷰 컨트롤러가 밀고있는 채팅을 나열 내있는 tableview의 행 중 하나에 도청
  • 되면 사용 가능한 모든 채팅 창을 나열하는 (의는 "목록"을 부르 자). 이보기 컨트롤러는 채팅 자동으로 탭 표시 줄을 숨 깁니다하지만 탐색 컨트롤러 사용자가 채팅 목록으로 돌아갑니다 뒤로 버튼을 탭
  • 의 뒤로 버튼을 계속 창 탭 표시 줄이
을 다시 나타납니다입니다

올바른 채팅 화면을 표시하려면 다음 코드를 사용하고 있습니다. 뒤로 버튼을 탭하면 예상대로 채팅 목록으로 돌아갑니다. 유일한 문제는 탐색 컨트롤러가 탭 컨트롤러에 포함되어 있지 않다는 것입니다. 이 코드는 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler에서 호출되는 :

let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let initialVC : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "ChatVC") as UIViewController 
let detailVC : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "ChatDetailVC") as UIViewController 
let navContr = UINavigationController(rootViewController:initialVC) 
self.window = UIWindow(frame: UIScreen.main.bounds) 
self.window?.rootViewController = navContr 
self.window?.makeKeyAndVisible() 
initialVC.navigationController?.pushViewController(detailVC, animated: true) 

탭 컨트롤러가 스택의 최상위 수준이되도록 어떻게 내 코드를 구성 할 수 있습니다? 내 스토리 보드에 "TabController"라는 레이블이 붙어 있습니다.

+0

사용자가 알림을 누를 때 수동으로 각 탐색을 수행해야합니다. –

+0

[this] (https://stackoverflow.com/a/47131085/6680583)와 같은 것을 시도해보십시오. – Mannopson

답변

0

당신은 단지 시작 일반적으로 사용자가 알림 탭에서 오는 경우를 판정합니다 플래그을 만들거나 할 필요가

struct NotificationEvent { 
    static var isFromNotification = false 
} 

컨트롤러의 스택입니다 -

TabBarController-> UINavigationController-> ChatListViewController -> ChatDetailViewController

In AppDelegate func userNotificationCenter -

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let vc = storyboard.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController//Your Tabbarcontroller 
NotificationEvent.isFromNotification = true//recognize is from notification 
window?.rootViewController = vc 

ChatListViewController

class ChatListViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     if NotificationEvent.isFromNotification { 
      openChatDetailScreen()//Jumps to that chat screen 
      NotificationEvent.isFromNotification = false 
     } 
    } 
    @IBAction func tapsOnChatList(_ sender: UIButton) {//consider it as didselect method in your tableView 
     openChatDetailScreen() 
    } 

    func openChatDetailScreen() { 
     let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatDetailViewController") as! ChatDetailViewController 
     navigationController?.pushViewController(vc, animated: true) 
    } 
} 

또는 대신 NotificationEvent.isFromNotification 당신은 또한 특정의 ViewController에서 알림 이벤트의 UIApplicationLaunchOptionsKey.remoteNotification/관찰자로 확인할 수 있습니다.

관련 문제