问题
Setting up a page view inside of a container. Followed all instructions I could find (beginner).
These two arrays are in the main view controller which have names of the images I want to populate the pages.
self.shirtImage = NSArray(objects: "shirt1", "shirt2")
self.pantsImage = NSArray(objects: "pants1", "pants2")
Whenever I run the program, these two lines:
self.todayShirt.image = UIImage(named: self.shirtName)
self.todayPants.image = UIImage(named: self.pantsName)
return the error fatal error: unexpectedly found nil while unwrapping an Optional value
or CUICatalog: Invalid asset name supplied:
which means somehow they are receiving a nil value from the arrays.
The page content class looks like this:
class TodayPicksViewController: UIViewController {
@IBOutlet weak var todayShirt: UIImageView!
@IBOutlet weak var todayPants: UIImageView!
var pageIndex: Int!
var shirtName: String!
var pantsName: String!
override func viewDidLoad() {
super.viewDidLoad()
self.todayShirt.image = UIImage(named: self.shirtName)
self.todayPants.image = UIImage(named: self.pantsName)
and the main view controller is set up like this:
class ViewController: UIViewController, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController!
var shirtImage: NSArray!
var pantsImage: NSArray!
override func viewDidLoad() {
super.viewDidLoad()
self.shirtImage = NSArray(objects: "shirt1", "shirt2")
self.pantsImage = NSArray(objects: "pants1", "pants2")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let startVC = self.viewControllerAtIndex(0) as TodayPicksViewController
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers((viewControllers as! [UIViewController]), direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.height - 60)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
func viewControllerAtIndex(index: Int) -> TodayPicksViewController {
if ((self.shirtImage.count == 0) || index >= self.shirtImage.count){
return TodayPicksViewController()
}
let vc: TodayPicksViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TodayPicksViewController") as! TodayPicksViewController
vc.shirtName = self.shirtImage[index] as! String
vc.pantsName = self.pantsImage[index] as! String
vc.pageIndex = index
return vc
}
I have another project that has a working page view similar to this and everything works perfectly and I have copied over almost everything exactly so I cannot figure out why the value from the main view controller is not properly passing to the page view in this one. Any assistance is appreciated.
回答1:
I think I've found your problem, is your code entering in this if-statement?
if ((self.shirtImage.count == 0) || index >= self.shirtImage.count){
return TodayPicksViewController()
}
If so, shirtName
and pantsName
will never have been initialised.
回答2:
Something is going wrong when you change the image of the UIImageView. Try todayShirt.image = UIImage(imageLiteral: "\(shirtName)")
and todayPants.image = UIImage(imageLiteral: "\(pantsName)")
.
Then be very very sure that you have 4 images inserted in your XCode project: shirt1.png
, shirt2.png
, pants1.png
, and pants2.png
. It's also ok if they are shirt1.jpg
, etc.
If this still isn't helping, try changing your array-creation method. Instead of using NSArray and only adding objects to it in the viewdidload()
section, just create the arrays & add objects in the global scope. So, right now where you have var shirtImage: NSArray
and var shirtImage: NSArray
, remove both of those and add var shirtsArray:NSMutableArray = ["shirt1", "shirt2"]
and var pantsArray:NSMutableArray = ["pants1", "pants2"]
.
来源:https://stackoverflow.com/questions/36560473/uiimage-name-from-array-keeps-returning-nil-value