ios - How to get my UIPageViewController to load the correct index? Swift 3 -
this first post, open feedback if can improve question.
i have been studying swift year , trying build app uses uipageviewcontroller make app mimics experience of book. user able create many pages want. now, have template uiviewcontroller imageview , textview. user presses "+" button add next page, should appear pre-set blank imageview (that can set) , blank textview.
specs: i'm persisting user information coredata.
the problem: create new set of pages. add image , text. tap "+" , same on next page. when tap "+" after that, next page , each page after copies image , text previous page rather being empty page.
here logic how pages should run.
extension bookpageviewcontroller: uipageviewcontrollerdatasource { func pageviewcontroller(_ pageviewcontroller: uipageviewcontroller, viewcontrollerbefore viewcontroller: uiviewcontroller) -> uiviewcontroller? { guard let viewcontrollerindex = storypageviewcontrollers.index(of: viewcontroller as! templateviewcontroller) else { return nil } if viewcontrollerindex == 0 { return nil } // page won't scroll below first page. let previousindex = viewcontrollerindex - 1 guard storypageviewcontrollers.count > previousindex else { return nil } currentpage = double(previousindex) return storypageviewcontrollers[previousindex] } func pageviewcontroller(_ pageviewcontroller: uipageviewcontroller, viewcontrollerafter viewcontroller: uiviewcontroller) -> uiviewcontroller? { guard let viewcontrollerindex = storypageviewcontrollers.index(of: viewcontroller as! templateviewcontroller) else { return nil } let nextindex = viewcontrollerindex + 1 let storypageviewcontrollerscount = storypageviewcontrollers.count guard storypageviewcontrollerscount != nextindex else { return nil } guard storypageviewcontrollerscount > nextindex else { return nil } currentpage = double(nextindex) return storypageviewcontrollers[nextindex] } func presentationcount(for pageviewcontroller: uipageviewcontroller) -> int { return storypageviewcontrollers.count } func presentationindex(for pageviewcontroller: uipageviewcontroller) -> int { guard let firstviewcontroller = viewcontrollers?.first, let firstviewcontrollerindex = storypageviewcontrollers.index(of: firstviewcontroller as! templateviewcontroller) else { return 0 } return firstviewcontrollerindex } }
this ibaction "+" button:
guard let book = book else { return } pagecontroller.shared.createpagewith(text: "", pagenumber: currentpage + 1.0, image: #imageliteral(resourcename: "imageviewplaceholder"), book: book) updatestorypageviewcontrollers() setviewcontrollers([storypageviewcontrollers[int(currentpage) + 1]], direction: .forward, animated: true, completion: nil)
update: requested, here functions commenters asked for.
func updatestorypageviewcontrollers() { // mark: - return here talk spencer fixing vc updating. // make check see pages have view controllers, , make new view controller pages don't. guard let book = book else { return } // create array of view controllers store page vcs being instantiated var temporaryviewcontrollers: [templateviewcontroller] = [] // loop through books pages, , make view controller each if let pages = book.pages?.array as? [page], pages.count > 0 { page in pages { let storyboard = uistoryboard(name: "main", bundle: nil) guard let pagevc = storyboard.instantiateviewcontroller(withidentifier: "templateviewcontroller") as? templateviewcontroller else { break } pagevc.page = page temporaryviewcontrollers.append(pagevc) } } else { //fixme: - default image set here. guard let page = pagecontroller.shared.createpagewith(text: "", pagenumber: 0, image: #imageliteral(resourcename: "imageviewplaceholder"), book: book) else { return } let storyboard = uistoryboard(name: "main", bundle: nil) guard let pagevc = storyboard.instantiateviewcontroller(withidentifier: "templateviewcontroller") as? templateviewcontroller else { return } pagevc.page = page temporaryviewcontrollers.append(pagevc) } // set self.storypageviewcontrollers array array made , filled. self.storypageviewcontrollers = temporaryviewcontrollers }
here code createpagewith method:
@discardableresult func createpagewith(text: string, pagenumber: double, image: uiimage, book: book) -> page? { //turns uiimage data saved in coredata. guard let data = uiimagejpegrepresentation(image, 0.8) else { return nil } let page = page(text: text, pagenumber: pagenumber, photodata: data, book: book) bookcontroller.shared.savetopersistentstore() return page }
wiki
Comments
Post a Comment