我的 UIPageViewController 不断崩溃
My UIPageViewController keeps crashing
我花了几个小时尝试使用仪器和 Whosebug 对此进行调试,但我还没有找到解决方案。我的UIPageViewController老是崩溃,我真的认为是因为CPU快到100%了,内存能达到高位。但是,我找不到任何变量或任何强引用来阻止以前未显示的视图控制器解除分配。我认为这是 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
}
}
更新:初始视图控制器是"HarishIntroduction."
您的做法不正确,您正在尝试使用初始视图控制器实例化 UIPageViewController 作为视图控制器,而该视图控制器又是 UIPageViewController 的父级。
我在一个单独的 class 上执行此操作并且一切正常,尽管在页面控制器层次结构中我只创建了两个视图控制器。您可以添加更多,这没有问题。
并且没有必要在 viewDidLoad 的 main_queue 上做 dispatch_async,因为你已经在 main_queue with viewDidLoad 中,拿走那个代码。
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
}
}
}
上面的代码对我来说效果很好,如果需要我可以为你上传项目。
我花了几个小时尝试使用仪器和 Whosebug 对此进行调试,但我还没有找到解决方案。我的UIPageViewController老是崩溃,我真的认为是因为CPU快到100%了,内存能达到高位。但是,我找不到任何变量或任何强引用来阻止以前未显示的视图控制器解除分配。我认为这是 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
}
}
更新:初始视图控制器是"HarishIntroduction."
您的做法不正确,您正在尝试使用初始视图控制器实例化 UIPageViewController 作为视图控制器,而该视图控制器又是 UIPageViewController 的父级。
我在一个单独的 class 上执行此操作并且一切正常,尽管在页面控制器层次结构中我只创建了两个视图控制器。您可以添加更多,这没有问题。
并且没有必要在 viewDidLoad 的 main_queue 上做 dispatch_async,因为你已经在 main_queue with viewDidLoad 中,拿走那个代码。
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
}
}
}
上面的代码对我来说效果很好,如果需要我可以为你上传项目。