ios - performSegueWithIdentifier not working when being called from new instance of viewController in Swift -
my mission
when app receive notification , user taps on notification want redirect user correct view. in case, singleapplicationviewcontroller.
current code
pushnotification.swift - class static functions handle behaviors when receiving push notifications
the __getnavigationcontroller returns specific navigationcontroller based on tab -and viewindex tabbarcontroller.
internal static func __getnavigationcontroller(tabindex: int, viewindex: int) -> uinavigationcontroller { let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let window:uiwindow? = (uiapplication.sharedapplication().delegate?.window)! let storyboard = uistoryboard(name: "main", bundle: nil) let viewcontroller = storyboard.instantiateviewcontrollerwithidentifier("mainentry") window?.rootviewcontroller = viewcontroller let rootviewcontroller = appdelegate.window!.rootviewcontroller as! uitabbarcontroller rootviewcontroller.selectedindex = tabindex let nav = rootviewcontroller.viewcontrollers![viewindex] as! uinavigationcontroller return nav }
the applicationclicked being called when user click on notification , method calls on __getapplication fetch application db objectid received in push notification , instantiate grouptableviewcontroller perform segue singleapplicationviewcontroller.
(tabbarcontroller -> navigation controller -> grouptableviewcontroller -> singleapplicationviewcontroller)
what bit strange when set tabindex 0 , viewindex 1. groupview on second tab (tab 1) , view controller should first (0). when set them corresponding numbers, receive nil , application crashes.
i read force view controller load when doing _ = grouptableviewcontroller.view
, does. when being called, viewdidload -function being called.
/************** application ***************/ static func applicationclicked(objectid: string) { __getapplication(objectid) { (application, error) in if application != nil && error == nil { let nav = __getnavigationcontroller(0, viewindex: 1) let grouptableviewcontroller = nav.viewcontrollers.first as! groupstableviewcontroller _ = grouptableviewcontroller.view grouptableviewcontroller.performseguewithidentifier("grouptabletoapplicationtodetailapplication", sender: application!) } else { // hanlde error } } }
grouptableviewcontroller.prepareforsegue()
here create new instance of applicationtableviewcontroller, middle step before getting singleapplicationviewcontroller
} else if segue.identifier == "grouptabletoapplicationtodetailapplication" { let navc = segue.destinationviewcontroller as! uinavigationcontroller let controller = navc.topviewcontroller as! applicationviewcontroller controller.performseguewithidentifier("applicationstosingleapplicationsegue", sender: sender as! application) }
so, what's not working?
well, prepareforsegue in grouptableviewcontroller not being called. use the same code structure on timelineviewcontroller, , exact same code, when getting push notification , works perfectly. in case use tabindex 0 , viewindex 0 proper navigationcontroller.
please, thoughts and/or suggestions more welcome!
there change in following method..
internal static func __getnavigationcontroller(tabindex: int) -> uinavigationcontroller { let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let window:uiwindow? = (uiapplication.sharedapplication().delegate?.window)! let storyboard = uistoryboard(name: "main", bundle: nil) let viewcontroller = storyboard.instantiateviewcontrollerwithidentifier("mainentry") window?.rootviewcontroller = viewcontroller let rootviewcontroller = appdelegate.window!.rootviewcontroller as! uitabbarcontroller rootviewcontroller.selectedindex = tabindex let nav = rootviewcontroller.selectedviewcontroller as! uinavigationcontroller //this return navigation controller.. //no need of viewindex.. return nav }
you have written
let nav = rootviewcontroller.viewcontrollers![viewindex] as! uinavigationcontroller
change rootviewcontroller.selectedviewcontroller
give uinavigationcontroller
.
here navigavtion controller object..in applicationclicked method nav object might nil can not execute further performsegue code.
check following method.
/************** application ***************/ static func applicationclicked(objectid: string) { __getapplication(objectid) { (application, error) in if application != nil && error == nil { let nav = __getnavigationcontroller(0)//0 tab index..if want 1 replace 1 let grouptableviewcontroller = nav.viewcontrollers.first as! groupstableviewcontroller //rootview controller of nav controller grouptableviewcontroller.performseguewithidentifier("grouptabletoapplicationtodetailapplication", sender: application!) //perform seque root vc... } else { // hanlde error } } }
Comments
Post a Comment