容器中的页面视图不显示内容

Page view in Container not showing content

我正在尝试使用容器将页面视图放置到主视图上。由于我是 swift 的新手,所以我关注了一个视频来制作测试页面视图。我让它在测试中工作,然后复制了大部分代码并更改了相关变量。当我 运行 应用程序时,容器显示没有任何内容。

这是我的页面内容视图控制器。

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)



    // Do any additional setup after loading the view.
}

这是我的主视图控制器。

class ViewController: UIViewController, UIPageViewControllerDataSource {

var pageViewController: UIPageViewController!
var shirtImages: NSArray!
var pantsImages: NSArray!


override func viewDidLoad() {
    super.viewDidLoad()

    self.shirtImages = NSArray(objects: "shirt1", "shirt2")
    self.pantsImages = 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)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func viewControllerAtIndex(index: Int) -> TodayPicksViewController {
    if ((self.shirtImages.count == 0) || index >= self.shirtImages.count){
        return TodayPicksViewController()
    }

    let vc: TodayPicksViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! TodayPicksViewController
    vc.pantsName = self.pantsImages[index] as! String
    vc.shirtName = self.shirtImages[index] as! String
    vc.pageIndex = index


    return vc

}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    let vc = viewController as! TodayPicksViewController
    var index = vc.pageIndex as Int

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

    index -= 1

    return self.viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    let vc = viewController as! TodayPicksViewController
    var index = vc.pageIndex as Int

    if (index == NSNotFound){
        return nil
    }
    index += 1

    if (index == self.shirtImages.count){
        return nil
    }

    return self.viewControllerAtIndex(index)

}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return self.shirtImages.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}

我尝试了各种更改,希望能有所作为,最近我能得到返回的错误消息 CUICatalog: Invalid asset name supplied:

我查看了错误消息,它表明我可能将 nil 值传递给图像视图,但据我所知,它们的设置与我所做的测试项目完全相同。也许它与 parent/child 关系有关,但我不确定。非常感谢任何指导。

这行在不应该的时候被注释掉了:

// self.view.addSubview(self.pageViewController.view)

有了这条评论,您实际上并没有将 UIPageViewController 添加到屏幕上。