Hi am following a tutorial on UIPageViewController to generate a gallery of pictures in my project http://www.theappguruz.com/blog/easy-steps-to-implement-uipageviewcontroller-in-swift even download the demo and it works but in my if I get an error and the application falls.
fatal error: unexpectedly found nil while unwrapping an Optional value
at next my code.
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
}
}
var strPhotoName: String!
It is literally nil. You will need to assign it a image name. Let's say your image is called cloudsImage.png
You would set your var strPhotoName like this in viewDidLoad if you want.
override func viewDidLoad() {
super.viewDidLoad()
strPhotoName = "cloudsImage.png"
imageView.image = UIImage(named: strPhotoName)
lblTitle.text = strTitle
}
I believe Joakin is right, you are forcing the unwrap of an optional value, you need to either initialize your values when you declare them or treat them as optionals and unwrap them as you need them.
Try this:
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
}
}
Then, somewhere in your code you need to specify the name of the image (make sure the image exists in your project) and the name of the title. If you dont specify them, the updated lines of code will not get executed.
Read more about optional values here
import UIKit
class PageContentViewController: UIViewController {
@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
}
}
the main error was that the class of the page was wrong now the problem is solved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.