ios - Destroy previous viewController when presenting a new one -


i have application sidebarmenu has 5 sections. in storyboard i've created start mainviewcontroller embedded in navigationcontroller. i've created same 5 vc's , again embed them in navigationcontroller. have 6 vc's, each of wrapped in own navigationcontroller.

each vc implements sidebardelegate function has index of selected menu.

here code:

// sidebartableviewcontroller  protocol sidebartableviewcontrollerdelegate {     func sidebarcontroldidselectrow(indexpath: nsindexpath) }  class sidebartableviewcontroller: uitableviewcontroller {  // ...      override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) {         tableview.deselectrowatindexpath(indexpath, animated: false)         println(indexpath.row)         delegate?.sidebarcontroldidselectrow(indexpath)     } }   // sidebar class ------------------------------------------------  @objc protocol sidebardelegate {     func sidebardidselectbuttonatindex(index: int)     optional func sidebarwillclose()     optional func sidebarwillopen() }  class sidebar: nsobject, sidebartableviewcontrollerdelegate {  // ...      func sidebarcontroldidselectrow(indexpath: nsindexpath) {         println(indexpath.row)         delegate!.sidebardidselectbuttonatindex(indexpath.row)     } } 

then in function want present needed viewcontroller:

func sidebardidselectbuttonatindex(index: int) {     presentviewcontrollerbyindex(index) }  func presentviewcontrollerbyindex(index: int) {     let storyboard: uistoryboard = uistoryboard(name: "main", bundle: nil)     switch index {     case 0:         var viewcontroller = storyboard.instantiateviewcontrollerwithidentifier("searchvc") as! searchviewcontroller         viewcontroller.delegate = self         self.presentviewcontroller(viewcontroller, animated: false, completion: nil)         break      // same code here other 4 vc's storyboardid's      default: break     }  } 

then in presented view controller if user select section in menu should destroy vc before presenting selected viewcontroller? because when run application , started switching between controllers application memory in xcode increases (screenshot).

when saw i've started googling , found this answer. have created protocol in first menu vc test.

protocol myprotocol {     func dissmissandpresend(index: int) -> void } 

and when need present new vc call:

self.delegate?.dissmissandpresend(index) 

then in mainvc implement protocol , trying dismiss previous vc , present new one:

class mainviewcontroller: uiviewcontroller, sidebardelegate, myprotocol {  func dissmissandpresend(index: int) {     self.dismissviewcontrolleranimated(false, completion: {         self.presentviewcontrollerbyindex(index)     }) } 

now when launch program , started click on first menu item opens new vc memory increases before. , navigationcontroller presented view has gone in storyboard presented vc embedded in navigationcontroller.

what doing wrong? can me?

update::-------------------------------

here i've tried:

class mainviewcontroller: uiviewcontroller, sidebardelegate, searchvcprotocol {     var searchvc: searchviewcontroller!     var searchnavcontroller: shadowuinavigationcontroller!      override func viewdidload() {         let mainstoryboard: uistoryboard = uistoryboard(name: "main", bundle: nil)         searchvc = mainstoryboard.instantiateviewcontrollerwithidentifier("searchvc") as! searchviewcontroller         searchvc.delegate = self         searchnavcontroller = shadowuinavigationcontroller(rootviewcontroller: searchvc)     }      func sidebardidselectbuttonatindex(index: int) {         switch index {         case 0:             println("asda")             self.presentviewcontroller(searchnavcontroller, animated: false, completion: nil)             break         }     } 

then takes user searchviewcontroller , if user open menu again , click on searchviewcontroller again. how can dismiss , reopen. here i've tried doesn't work:

in seachviewcontroller i've created protocol:

protocol searchvcprotocol {     func dismissandpresent(index: int) -> void } 

then i've added variable delegate:

var delegate: searchvcprotocol? 

then when user selected menu item fires event:

func sidebardidselectbuttonatindex(index: int) {     delegate?.dismissandpresent(index) } 

and in mainviewcontroller i've implemented protocol , created dismissandpresent method don't know how restart presented viewcontroller.

class mainviewcontroller: uiviewcontroller, sidebardelegate, searchvcprotocol {     func dismissandpresent(index: int) {         // code restart vc     } } 

what should code restarting presented vc?

you calling instantiateviewcontrollerwithidentifier every time, creates ("instantiates") every time new viewcontroller. better once, keep reference , re-use on demand.

here how can keep reference. first create new variable on class level:

class myclass {     var viewcontroller: searchviewcontroller! 

then, in viewdidload, instantiate view controller , assign variable.

func viewdidload() {     viewcontroller = storyboard.instantiateviewcontrollerwithidentifier("searchvc") as! searchviewcontroller } 

viewdidload called once not happen anymore creating multiple instances.

then can reuse viewcontroller again , again:

func presentviewcontrollerbyindex(index: int) {     let storyboard: uistoryboard = uistoryboard(name: "main", bundle: nil)     switch index {     case 0:         // here can reuse formerly created viewcontroller          viewcontroller.delegate = self         self.presentviewcontroller(viewcontroller, animated: false, completion: nil)         break      // same code here other 4 vc's storyboardid's      default: break     }  } 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -