swift - Best way to switch UIViewControllers based on different input in an ios application -
what trying change views based on data. demonstrate trying have constructed little example demonstrate. used uisegmentedcontrol simulate data data when button pressed, switch between views based on value of uisegmentedcontrol. here storyboard...

you can see there base view uisegmentedcontrol, there button on same view trigger switch of view controllers based on state of uisegmentedcontrol 1 of 3 view controllers on right. have included class of base uiviewcontroller can see how achieve switching of views.
import uikit class rootviewcontroller: uiviewcontroller { var nextviewidentifier: string? = nil @ibaction func switchviewbuttonpressed(sender: uibutton) { if (nextviewidentifier != nil) { let nextview = self.storyboard?.instantiateviewcontrollerwithidentifier(nextviewidentifier!) as! uiviewcontroller self.showviewcontroller(nextview, sender: self) } } @ibaction func valuechanged(sender: uisegmentedcontrol) { switch sender.selectedsegmentindex { case 0: nextviewidentifier = "view1" case 1: nextviewidentifier = "view2" case 2: nextviewidentifier = "view3" default: nextviewidentifier = nil } } } so, first function switchviewbuttonpressed called when button pressed , switch view based on variable nextviewidentifier. second function valuechanged changes nextviewidentifier variable when button on uisegmentedcontrol pushed.
all of code works, doesn't seem natural way implement in storyboard format. seem there should else achieve effect looking for. asking if has better way change views based on data not known in advance , when these views different. way assume stacking different views in 1 view controller , picking 1 want through prepareforsegue method. anyway, suggestions best way switch view controllers in context appreciated. thanks.
use container view "embed" segues. execute segues when press buttons on segmented control. more info on container views:
Comments
Post a Comment