0

로컬 알림 이 실행되고 해당 사용자 지정 동작이 수행 될 때마다에서 특정 ViewController을 열려고합니다. , second()TabBarController에서 특정 ViewController 표시

func second() { 


    let storyboard = UIStoryboard.init(name: "Main", bundle: nil) 
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController 
    let navigationController = storyboard.instantiateViewController(withIdentifier: "Second") as! UINavigationController 

    tabBarController.present(navigationController, animated: true) { 

    } 

    self.window = UIWindow.init(frame: UIScreen.main.bounds) 
    self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0) 
    self.window?.rootViewController = tabBarController 
    self.window?.makeKeyAndVisible() 

} 

그리고 그것은 잘 작동 것 :

func first() { 


    let storyboard = UIStoryboard.init(name: "Main", bundle: nil) 
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController 
    let navigationController = storyboard.instantiateViewController(withIdentifier: "First") as! UINavigationController 

    tabBarController.present(navigationController, animated: true) { 

    } 

    self.window = UIWindow.init(frame: UIScreen.main.bounds) 
    self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0) 
    self.window?.rootViewController = tabBarController 
    self.window?.makeKeyAndVisible() 

} 

둘째 기능 :

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    switch response.actionIdentifier { 
    case "first": 
     DispatchQueue.main.async(execute: { 
      self.first() 
     }) 
    case "second": 
     DispatchQueue.main.async(execute: { 
      self.second() 
     }) 
    default: 
     break 
    } 


    completionHandler() 
} 

따라서는 first()기능이다 : 나는 코드의 다음 줄을 사용했다 하지만 나는 열 수 없다. 두 번째 ViewController 첫 번째 제시하고 두 번째 통지 해고 상태 : 콘솔에서 다음의 ViewController를 제시 경고 시도 ...

답변

1

사용이 :

tabBarController.selectedIndex = 1 

경우 1viewcontrollers 님이 제출 한 것일 수도 있습니다. tabBarController

+0

그것은 쓸모없는 것처럼 보입니다. 오직 1을 반환합니다 – Mannopson

+1

아무 것도 반환 할 필요가 없습니다. selectedIndex를 임의의 숫자로 설정하면 탭 막대의 탭이 선택된 색인으로 변경됩니다. selectedIndex = 0으로 설정하면 첫 번째 탭으로 전환됩니다. selectedIndex = 1, 두 번째 탭. 등등 –

+0

도와 줘서 고마워. UITabBarController 클래스에 대한 경험이 없습니다. – Mannopson

1

비슷한 문제가 발생하여 selectedIndex을 변경했습니다. 나를 위해 일하지 않아. 프로젝트의 요구 사항에 따라 ViewController으로 인스턴스화하고 Subview으로 추가하고 Subview으로 이동할 수 있습니다. 개봉 된 후에는 Subview을 삭제해야합니다.

let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView") 
    self.addChildViewController(replyView!) 
    self.view.addSubview(replyView!.view) 
    replyView!.didMoveToParentViewController(self) 
+0

어떻게 끝났습니까? 이 코드를 어디에서 불러야합니까? – Mannopson

+1

당신이'ViewController'를 바꿔서 어떤 동작을하고자하는지. 버튼 액션이 있다면 버튼 액션 내에서 위 코드를 호출합니다. –

+0

대단히 감사합니다! – Mannopson

관련 문제