数组中的UIImage名称不断返回nil值. [英] UIImage name from array keeps returning nil value.

查看:77
本文介绍了数组中的UIImage名称不断返回nil值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在容器内部设置页面视图.遵循了我可以找到的所有说明(初学者).

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)

返回错误fatal error: unexpectedly found nil while unwrapping an Optional valueCUICatalog: Invalid asset name supplied:,这意味着它们以某种方式从数组接收nil值.

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.

页面内容类如下:

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.

推荐答案

更改UIImageView的图像时出现了问题.尝试todayShirt.image = UIImage(imageLiteral: "\(shirtName)")todayPants.image = UIImage(imageLiteral: "\(pantsName)").

Something is going wrong when you change the image of the UIImageView. Try todayShirt.image = UIImage(imageLiteral: "\(shirtName)") and todayPants.image = UIImage(imageLiteral: "\(pantsName)").

然后非常确定,您已在XCode项目中插入了4张图像:shirt1.pngshirt2.pngpants1.pngpants2.png.如果它们是shirt1.jpg等,也可以.

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.

如果仍然不能解决问题,请尝试更改数组创建方法.只需使用创建数组&而不是使用NSArray并仅在viewdidload()部分中向其添加对象即可.在全局范围内添加对象.因此,现在在具有var shirtImage: NSArrayvar shirtImage: NSArray的位置,将它们都删除,并添加var shirtsArray:NSMutableArray = ["shirt1", "shirt2"]var pantsArray:NSMutableArray = ["pants1", "pants2"].

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"].

这篇关于数组中的UIImage名称不断返回nil值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆