ios - Tried to pop to a view controller that doesn't exist -


i'm trying return previous view controller i'm running issues (to understanding) shouldn't happening.

a short description of i'm trying do: have 4 view controllers: a, b, c , d. basic ui flow -> b -> c -> d. after finishing work in c, want return b.

my code:

let viewcontrollerarray = self.navigationcontroller?.viewcontrollers                 for(var i=0;i<viewcontrollerarray?.count;i++){                     if(viewcontrollerarray![i].iskindofclass(inventorylistviewcontroller)){                         self.navigationcontroller?.poptoviewcontroller(viewcontrollerarray![i], animated: true)                     }                 } 

this works fine if b still exists on navigationcontroller's stack. if b has been removed stack (due memory-related reasons) gives me tried pop view controller doesn't exist error (obviously). thing i'm confused shouldn't if-statement prevent poptoviewcontroller method getting called if b doesn't exist on stack anymore?

the best way prevent crash optional unwrapping. try code , let me know if solves issue.

let allvc = self.navigationcontroller?.viewcontrollers  if  let inventorylistvc = allvc![allvc!.count - 2] as? inventorylistviewcontroller {    self.navigationcontroller!.poptoviewcontroller(inventorylistvc, animated: true) } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -