gpt4 book ai didi

ios - Swift 中的 UIPageViewController

转载 作者:行者123 更新时间:2023-11-28 12:47:38 26 4
gpt4 key购买 nike

您好,我正在按照 UIPageViewController 上的教程在我的项目中生成图片库 http://www.theappguruz.com/blog/easy-steps-to-implement-uipageviewcontroller-in-swift甚至下载了演示版,它也能正常工作,但在我的应用程序中,如果出现错误并且应用程序失败。

fatal error: unexpectedly found nil while unwrapping an Optional value

enter image description here

接下来是我的代码。

PageContentViewController.swift:

import UIKit

class PageContentViewController: UIPageViewController {


@IBOutlet weak var lblTitle: UILabel!

@IBOutlet weak var imageView: UIImageView!


var pageIndex: Int = 0
var strTitle: String!
var strPhotoName: String!


override func viewDidLoad() {
super.viewDidLoad()

imageView.image = UIImage(named: strPhotoName)
lblTitle.text = strTitle

}
}

ViewController2.swift

import UIKit

class ViewController2: UIPageViewController, UIPageViewControllerDataSource
{
var arrPageTitle: NSArray = NSArray()
var arrPagePhoto: NSArray = NSArray()

override func viewDidLoad() {
super.viewDidLoad()

arrPageTitle = ["This is The App Guruz", "This is Table Tennis 3D", "This is Hide Secrets"];
arrPagePhoto = ["1.jpg", "2.jpg", "3.jpg"];

self.dataSource = self

self.setViewControllers([getViewControllerAtIndex(0)] as [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}

// MARK:- UIPageViewControllerDataSource Methods

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
let pageContent: PageContentViewController = viewController as! PageContentViewController

var index = pageContent.pageIndex

if ((index == 0) || (index == NSNotFound))
{
return nil
}

index -= 1;
return getViewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
let pageContent: PageContentViewController = viewController as! PageContentViewController

var index = pageContent.pageIndex

if (index == NSNotFound)
{
return nil;
}

index += 1;
if (index == arrPageTitle.count)
{
return nil;
}
return getViewControllerAtIndex(index)
}

// MARK:- Other Methods
func getViewControllerAtIndex(index: NSInteger) -> PageContentViewController
{
// Create a new view controller and pass suitable data.
let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController

pageContentViewController.strTitle = "\(arrPageTitle[index])"
pageContentViewController.strPhotoName = "\(arrPagePhoto[index])"
pageContentViewController.pageIndex = index

return pageContentViewController
}



}

最佳答案

我相信 Joakin 是对的,您正在强制解包一个可选值,您需要在声明它们时初始化您的值,或者将它们视为可选值并在需要时解包它们。

试试这个:

var strTitle: String?
var strPhotoName: String?

override func viewDidLoad() {
super.viewDidLoad()
if let photoName = strPhotoName {
imageView.image = UIImage(named: photoName)
}

if let title = strTitle {
lblTitle.text = title
}
}

然后,您需要在代码中的某处指定图像的名称(确保该图像存在于您的项目中)和标题的名称。如果你不指定它们,更新的代码行将不会被执行。

阅读更多关于可选值的信息 here

关于ios - Swift 中的 UIPageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37533476/

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