gpt4 book ai didi

ios - 我的 UIPageViewController 不断崩溃

转载 作者:行者123 更新时间:2023-11-29 01:22:01 25 4
gpt4 key购买 nike

我花了几个小时尝试使用仪器和 StackOverFlow 对此进行调试,但我还没有找到解决方案。我的UIPageViewController一直闪退,我真的认为是CPU快到100%,内存能到高位了。但是,我找不到任何变量或任何强引用来阻止以前未显示的 View Controller 解除分配。我认为这是 PageViewController 中的内存问题,但我不是 100% 确定。我是 Swift 的新手,我真的很难尝试首次亮相,所以非常感谢任何帮助。这是我的 UIPageViewControllerDataSource 的代码。

import UIKit

class HarishIntroduction: UIViewController, UIPageViewControllerDataSource {

var pageViewController: UIPageViewController!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.pageViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
weak var initialViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishIntroduction") as? HarishIntroduction
let viewController = NSArray(object: initialViewController!)
dispatch_async(dispatch_get_main_queue(), {
self.pageViewController.setViewControllers(viewController as? [UIViewController], direction: .Forward, animated: false, completion: nil)
})
self.pageViewController.view.frame = self.view.bounds
self.addChildViewController(pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(HarishIntroduction) {
return nil
}
if viewController.isKindOfClass(HarishPlaces) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishIntroduction") as? HarishIntroduction
return correctViewController
}
if viewController.isKindOfClass(WyomingSeminary) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPlaces") as? HarishPlaces
return correctViewController
}
if viewController.isKindOfClass(AppleTree) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("WyomingSeminary") as? WyomingSeminary
return correctViewController
}
if viewController.isKindOfClass(KearneyHighSchool) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("AppleTree") as? AppleTree
return correctViewController
}
if viewController.isKindOfClass(MosconeCenter) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("KearneyHighSchool") as? KearneyHighSchool
return correctViewController
}
else {
return nil
}
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(HarishIntroduction) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPlaces") as? HarishPlaces
return correctViewController
}
if viewController.isKindOfClass(HarishPlaces) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("WyomingSeminary") as? WyomingSeminary
return correctViewController
}
if viewController.isKindOfClass(WyomingSeminary) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("AppleTree") as? AppleTree
return correctViewController
}
if viewController.isKindOfClass(AppleTree) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("KearneyHighSchool") as? KearneyHighSchool
return correctViewController
}
if viewController.isKindOfClass(KearneyHighSchool) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("MosconeCenter") as? MosconeCenter
return correctViewController
}
if viewController.isKindOfClass(MosconeCenter) {
return nil
}
else {
return nil
}
}

更新:初始 View Controller 是“HarishIntroduction”。

最佳答案

您所做的不是正确的方法,您正在尝试使用初始 View Controller 实例化 UIPageViewController 作为 View Controller ,而 View Controller 又是 UIPageViewController 的父级。

我在一个单独的类上执行此操作,一切正常,尽管在页面 Controller 层次结构中我只创建了两个 View Controller 。您可以添加更多,这没有问题。

并且不需要在 viewDidLoad 上的 main_queue 上执行dispatch_async,因为您已经在带有 viewDidLoad 的 main_queue 中,请删除该代码。

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource {

var pageViewController: UIPageViewController!
var harishStoryboard:UIStoryboard!

override func viewDidLoad() {
super.viewDidLoad()

self.harishStoryboard = self.storyboard

// Do any additional setup after loading the view.
self.pageViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let initialViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishIntroduction") as! HarishIntroduction
let viewController = NSArray(object: initialViewController)

self.pageViewController.setViewControllers(viewController as? [UIViewController], direction: .Forward, animated: false, completion: nil)
self.pageViewController.view.frame = self.view.bounds
self.addChildViewController(pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(HarishIntroduction) {
return nil
}
if viewController.isKindOfClass(HarishPlaces) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishIntroduction") as? HarishIntroduction
return correctViewController
}
/*if viewController.isKindOfClass(WyomingSeminary) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPlaces") as? HarishPlaces
return correctViewController
}
if viewController.isKindOfClass(AppleTree) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("WyomingSeminary") as? WyomingSeminary
return correctViewController
}
if viewController.isKindOfClass(KearneyHighSchool) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("AppleTree") as? AppleTree
return correctViewController
}
if viewController.isKindOfClass(MosconeCenter) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("KearneyHighSchool") as? KearneyHighSchool
return correctViewController
}*/
else {
return nil
}
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(HarishIntroduction) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPlaces") as? HarishPlaces
return correctViewController
}
/*if viewController.isKindOfClass(HarishPlaces) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("WyomingSeminary") as? WyomingSeminary
return correctViewController
}
if viewController.isKindOfClass(WyomingSeminary) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("AppleTree") as? AppleTree
return correctViewController
}
if viewController.isKindOfClass(AppleTree) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("KearneyHighSchool") as? KearneyHighSchool
return correctViewController
}
if viewController.isKindOfClass(KearneyHighSchool) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("MosconeCenter") as? MosconeCenter
return correctViewController
}
if viewController.isKindOfClass(MosconeCenter) {
return nil
}*/
else {
return nil
}
}
}

上面的代码对我来说效果很好,如果需要我可以为您上传项目。

关于ios - 我的 UIPageViewController 不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com