ios - How to call popToRootViewController() function from a separate view controller? -


this function pops user root in uinavigationcontroller:

func poptoroot() {     navigationcontroller?.poptorootviewcontroller(animated: true) } 

however, need call function tab bar, in entirely different class , file , not in navigation stack.

func movetotab3(sender: uibutton!) {     if currentcontentcontroller != containerstack[3] {         movetotab(3, animated: false)     } else {         // since in tab, poptoroot function goes here, how?     } } 

the problem .navigationcontroller? particular , appears has called within object resides. how done?

i'm not sure got right, make possible navigate root current controller, can detect top controller in app using this:

extension uiapplication {     class func topviewcontroller(controller: uiviewcontroller? = uiapplication.shared.keywindow?.rootviewcontroller) -> uiviewcontroller? {         if let navigationcontroller = controller as? uinavigationcontroller {             return topviewcontroller(controller: navigationcontroller.visibleviewcontroller)         }         if let tabcontroller = controller as? uitabbarcontroller {             if let selected = tabcontroller.selectedviewcontroller {                 return topviewcontroller(controller: selected)             }         }         if let presented = controller?.presentedviewcontroller {             return topviewcontroller(controller: presented)         }         return controller     } } 

then can access top controller , call poptoroot:

if let topcontroller = uiapplication.topviewcontroller() {         // navigate root                topcontroller.navigationcontroller?.poptorootviewcontroller(animated: true) } 

it should work. let me know if need more info


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -