这是我的第一篇文章,所以如果我能改进我的问题,我愿意接受反馈。
我一直在研究Swift大约一年了,我正在尝试构建一个使用UIPageViewController制作一个模仿书籍体验的应用程序的应用程序。用户可以根据需要创建任意数量的页面。现在,我有一个带有imageView和textView的模板UIViewController。用户按下" +"按钮添加下一页,它应该显示为预先设置的空白imageView(他们可以设置)和一个空白的textView。
规格: 我使用CoreData保存用户信息。
问题: 我创建了一组新的页面。然后我添加一个图像和一些文字。我轻拍" +"并在下一页上做同样的事情。当我点击" +"之后,下一页和之后的每个页面都会复制上一页中的图像和文本,而不是空页。
以下是我的网页应如何运行的逻辑。
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
}
}
这是" +"的IBAction。按钮:
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)
更新: 根据要求,以下是评论者要求的功能。
func updateStoryPageViewControllers() {
// Mark: - RETURN HERE TO TALK TO SPENCER ABOUT FIXING THE VC UPDATING.
// Make a check to see which pages already have view controllers, and only make a new view controller for the pages that don't.
guard let book = book else { return }
// Create an array of view controllers to store the page VCs being instantiated
var temporaryViewControllers: [TemplateViewController] = []
// Loop through the books pages, and make a view controller for each
if let pages = book.pages?.array as? [Page], pages.count > 0 {
for 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 the self.storyPageViewControllers array to the array you just made and filled.
self.storyPageViewControllers = temporaryViewControllers
}
以下是createPageWith方法的代码:
@discardableResult func createPageWith(text: String, pageNumber: Double, image: UIImage, book: Book) -> Page? {
//Turns the UIImage into data to be 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
}