2017-05-27 60 views
0
let appDelegate = UIKit.UIApplication.shared.delegate! 

     if let tabBarController = appDelegate.window??.rootViewController as? UITabBarController { 
      let storyboard = UIStoryboard.init(name: "Main", bundle: nil) 
      let signInVC = storyboard.instantiateViewController(withIdentifier: "SignInVC") as! SignInVC 

      guard !signInVC.isBeingPresented else { 
       log.warning("Attempt to present sign in sheet when it is already showing") 
       return 
      } 

      signInVC.modalPresentationStyle = UIModalPresentationStyle.formSheet 

      tabBarController.present(signInVC, animated: true, completion: nil) 
     } 

儘管存在signInVC,但可以多次調用此代碼。我已經添加了這個檢查:「嘗試呈現時已呈現」仍然顯示在檢查後?

guard !signInVC.isBeingPresented else { 
    log.warning("Attempt to present sign in sheet when it is already showing") 
    return 
} 

,但它似乎並沒有阻止這個錯誤:

Warning: Attempt to present <App.SignInVC: 0x101f2f280> on <UITabBarController: 0x101e05880> which is already presenting <App.SignInVC: 0x101f4e4c0> 

回答

1

guard不是有效的檢查。 isBeingPresented正在一個尚未出現的全新視圖控制器實例上調用。所以isBeingPresented將永遠是false。除此之外,該屬性只能在視圖控制器的view[Will|Did]Appear方法中使用。

你想檢查的是看看tabBarController是否已經呈現另一個視圖控制器或不。

最後,只有創建和設置登錄視圖控制器,如果它應該出現。

let appDelegate = UIKit.UIApplication.shared.delegate! 

if let tabBarController = appDelegate.window?.rootViewController as? UITabBarController { 
    if tabBarController.presentedViewController == nil { 
     let storyboard = UIStoryboard.init(name: "Main", bundle: nil) 
     let signInVC = storyboard.instantiateViewController(withIdentifier: "SignInVC") as! SignInVC 
     signInVC.modalPresentationStyle = UIModalPresentationStyle.formSheet 

     tabBarController.present(signInVC, animated: true, completion: nil) 
    } 
} 
+0

這是否解決了您的問題,還是仍然存在問題? – rmaddy

相關問題