ios - Set view controllers of UITabBarController in Swift -
i trying programatically set view controllers of custom tabbarcontroller:
import uikit class tabbarviewcontroller: uitabbarcontroller, uitabbarcontrollerdelegate { var cameraviewcontroller: uiviewcontroller? var profileviewcontroller: uiviewcontroller? override func viewdidload() { super.viewdidload() self.delegate = self //self.viewcontrollers = [cameraviewcontroller, profileviewcontroller] as! [anyobject]? let controllers: [uiviewcontroller?] = [cameraviewcontroller, profileviewcontroller] self.setviewcontrollers(controllers as! [anyobject], animated: true) }
but line
self.viewcontrollers = [cameraviewcontroller, profileviewcontroller] as! [anyobject]?
i error cannot convert [uiviewcontroller] [anyobject?]
and line
self.setviewcontrollers(controllers as! [anyobject], animated: true)
i error saying:
cannot invoke 'setviewcontrollers' argument list of type '([anyobject], animated: bool)'
my problem anyobject , typecasting guess.
the problem view controllers trying use declared optional:
var cameraviewcontroller: uiviewcontroller? var profileviewcontroller: uiviewcontroller?
so have 3 options:
don't make them optional. requires initialize them when initalize
tabbarviewcontroller
. maybe safest option.if know
cameraviewcontroller
,profileviewcontroller
nevernil
inviewdidload
:self.viewcontrollers = [cameraviewcontroller!, profileviewcontroller!]
check if
cameraviewcontroller
,profileviewcontroller
not nil inviewdidload
. smells bad design me though.if let c = cameraviewcontroller, let p = profileviewcontroller { self.viewcontrollers = [c, p] }
so boils down how initialize cameraviewcontroller
, profileviewcontroller
. set before tab bar view controllers shown? if so, recommend adding custom init
class.
Comments
Post a Comment