swift - UIPageViewController : move programmatically page doesn't work -
i try turn page touching button on 1 of 2 navigation controllers(each navigationcontroller related viewcontroller) when call function in pageviewcontroller change index , change page doesn't work @ all...
i tried setviewcontroller.
i like in snapchat app, navbuttonitem can slide left or right.
thank in advance
here code of pageviewcontroller :
class pageviewcontroller: uipageviewcontroller,uipageviewcontrollerdatasource, uipageviewcontrollerdelegate{ var index = 0 var identifiers: nsarray = ["firstnavigationcontroller", "secondnavigationcontroller"] override func viewdidload() { self.datasource = self self.delegate = self let startingviewcontroller = self.viewcontrolleratindex(self.index) let viewcontrollers: nsarray = [startingviewcontroller] self.setviewcontrollers(viewcontrollers [anyobject], direction: uipageviewcontrollernavigationdirection.forward, animated: false, completion: nil) } func viewcontrolleratindex(index: int) -> uinavigationcontroller! { let storyboard : uistoryboard = uistoryboard(name: "main", bundle:nil) //first view controller = firstviewcontrollers navigation controller if index == 0 { return storyboard.instantiateviewcontrollerwithidentifier("firstnavigationcontroller") uinavigationcontroller } //second view controller = secondviewcontroller's navigation controller if index == 1 { return storyboard.instantiateviewcontrollerwithidentifier("secondnavigationcontroller") uinavigationcontroller } return nil } func pageviewcontroller(pageviewcontroller: uipageviewcontroller, viewcontrollerafterviewcontroller viewcontroller: uiviewcontroller) -> uiviewcontroller? { let identifier = viewcontroller.restorationidentifier let index = self.identifiers.indexofobject(identifier!) //if index end of array, return nil since dont want view controller after last 1 if index == identifiers.count - 1 { return nil } //increment index viewcontroller after current index self.index = self.index + 1 return self.viewcontrolleratindex(self.index) } func pageviewcontroller(pageviewcontroller: uipageviewcontroller, viewcontrollerbeforeviewcontroller viewcontroller: uiviewcontroller) -> uiviewcontroller? { let identifier = viewcontroller.restorationidentifier let index = self.identifiers.indexofobject(identifier!) //if index 0, return nil since dont want view controller before first 1 if index == 0 { return nil } //decrement index viewcontroller before current 1 self.index = self.index - 1 return self.viewcontrolleratindex(self.index) } }
first, why don't stop each instance of controllers rather re-instantiating ? here line :
func viewcontrolleratindex(index: int) -> uinavigationcontroller! { let storyboard : uistoryboard = uistoryboard(name: "main", bundle:nil) //first view controller = firstviewcontrollers navigation controller if index == 0 { return storyboard.instantiateviewcontrollerwithidentifier("firstnavigationcontroller") uinavigationcontroller } //second view controller = secondviewcontroller's navigation controller if index == 1 { return storyboard.instantiateviewcontrollerwithidentifier("secondnavigationcontroller") uinavigationcontroller } return nil}
you create local array containing instances..
then, move programmatically between pages, can use setviewcontroller
way :
func showaviewcontroller(){ let startvc : nsarray = [viewcontroller] setviewcontrollers(startvc as! [anyobject], direction: .reverse, animated: true, completion: nil) }
where direction .reverse or .forward depending on position of destination view controller relative present view controller.
here example of test program wrote, worked me :
class rootvc: uipageviewcontroller, uipageviewcontrollerdatasource, uipageviewcontrollerdelegate { var viewcontroller1 : uiviewcontroller! var viewcontroller2 : uiviewcontroller! override func viewdidload() { super.viewdidload() datasource = self viewcontroller1 = storyboard?.instantiateviewcontrollerwithidentifier("viewcontroller1") as! uiviewcontroller viewcontroller1.title = "viewcontroller1" viewcontroller2 = storyboard?.instantiateviewcontrollerwithidentifier("viewcontroller2") as! uiviewcontroller viewcontroller2.title = "viewcontroller2" var startingvc : nsarray = [viewcontroller1] setviewcontrollers(startingvc as! [anyobject], direction: .forward, animated: false, completion: nil) notifications.addobserver(self, selector: "showviewcontroller1", name: "showviewcontroller1", object: nil) notifications.addobserver(self, selector: "showviewcontroller2", name: "showviewcontroller2", object: nil) } deinit{ notifications.removeobserver(self) } func showviewcontroller1(){ let startvc : nsarray = [viewcontroller1] setviewcontrollers(startvc as! [anyobject], direction: .forward, animated: true, completion: nil) } func showviewcontroller2(){ let startvc : nsarray = [viewcontroller1] setviewcontrollers(startvc as! [anyobject], direction: .forward, animated: true, completion: nil) } func pageviewcontroller(pageviewcontroller: uipageviewcontroller, viewcontrollerbeforeviewcontroller viewcontroller: uiviewcontroller) -> uiviewcontroller? { switch viewcontroller.title!{ case "viewcontroller1": return nil case "viewcontroller2": return viewcontroller1 default: return nil } } func pageviewcontroller(pageviewcontroller: uipageviewcontroller, viewcontrollerafterviewcontroller viewcontroller: uiviewcontroller) -> uiviewcontroller? { switch viewcontroller.title!{ case "viewcontroller1": return viewcontroller2 case "viewcontroller2": return viewcontroller1 default: return nil } }}
nsnotifications
is not mandatory, use avoid heavy delegation.
Comments
Post a Comment