在容器内设置页面视图。按照我能找到的所有说明(初学者)。
这两个数组位于主视图控制器中,它具有我想要填充页面的图像的名称。
self.shirtImage = NSArray(objects: "shirt1", "shirt2")
self.pantsImage = NSArray(objects: "pants1", "pants2")
每当我运行程序时,这两行:
self.todayShirt.image = UIImage(named: self.shirtName)
self.todayPants.image = UIImage(named: self.pantsName)
返回错误fatal error: unexpectedly found nil while unwrapping an Optional value
或CUICatalog: Invalid asset name supplied:
,这意味着它们以某种方式从数组中接收nil值。
页面内容类如下所示:
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)
并且主视图控制器设置如下:
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
}
我有另一个项目有一个类似于此的工作页面视图,一切都很完美,我几乎完全复制了所有内容,所以我无法弄清楚为什么主视图控制器的值没有正确传递到页面视图中这个。任何帮助表示赞赏。
答案 0 :(得分:0)
更改UIImageView的图像时出现问题。试试todayShirt.image = UIImage(imageLiteral: "\(shirtName)")
和todayPants.image = UIImage(imageLiteral: "\(pantsName)")
。
然后非常肯定您在XCode项目中插入了4张图片:shirt1.png
,shirt2.png
,pants1.png
和pants2.png
。如果它们是shirt1.jpg
等,也可以。
如果仍然没有帮助,请尝试更改阵列创建方法。不是使用NSArray而只是在viewdidload()
部分中添加对象,只需创建数组&在全局范围中添加对象。所以,现在您拥有var shirtImage: NSArray
和var shirtImage: NSArray
,删除这两个并添加var shirtsArray:NSMutableArray = ["shirt1", "shirt2"]
和var pantsArray:NSMutableArray = ["pants1", "pants2"]
。
答案 1 :(得分:0)
我认为我发现了您的问题,您的代码是否在此if语句中输入?
if ((self.shirtImage.count == 0) || index >= self.shirtImage.count){
return TodayPicksViewController()
}
如果是这样,shirtName
和pantsName
将永远不会被初始化。