ios - Same View controller in Tab bar controller for 4 tabs is giving wrong selected tabcontroller selected index -


i trying make template app... lets app loads same view controller has collectionview 4 tabs. according selected index, have load contents collection view. setting tab bar manually appdelegate. question is possible instantiating same viewcntroller 4 tabs of tabbarcontroller @ time. if yes, how know correctly index selected?

code tabbarcontroller in appdelegate

                self.window = uiwindow(frame: uiscreen.mainscreen().bounds)                 let tabbarcontroller = uitabbarcontroller()                  let storyboard : uistoryboard = uistoryboard(name: "main", bundle: nil)                  let firstimage = uiimage(named: "image1")                 let secondimage = uiimage(named: "image2")                 var controllers = [uiviewcontroller]()                 var = 0; < self.mylist.count; i++ {                   let vc : viewcontrollertwo = storyboard.instantiateviewcontrollerwithidentifier("view1") as! viewcontrollertwo                      if(i == 0 || == 3)                     {                         vc.tabbaritem = uitabbaritem(                             title: self.mylist[i],                             image: firstimage,                             tag: i)                         controllers.append(vc)                     }                     else                     {                         vc.tabbaritem = uitabbaritem(                             title: self.mylist[i],                             image: secondimage,                             tag: i)                         controllers.append(vc)                     }                   }                   self.tabbarcontroller.viewcontrollers = controllers                 self.window?.rootviewcontroller = self.tabbarcontroller                   self.self.window?.rootviewcontroller = self.tabbarcontroller                 self.window?.makekeyandvisible() 

if set class delegate tab bar controller, call didselectviewcontroller delegate method. can use controllers array determine index;

self.window = uiwindow(frame: uiscreen.mainscreen().bounds) let tabbarcontroller = uitabbarcontroller() tabbarcontroller.delegate = self   func tabbarcontroller(_ tabbarcontroller: uitabbarcontroller,    didselectviewcontroller viewcontroller: uiviewcontroller) {      if let index = self.controllers.indexof(viewcontroller) {         // index     } } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -