我正在尝试使用容器将页面视图放在主视图上。我跟着一个视频来制作测试页面视图,因为我是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值传递给图像视图但据我所知,它们的设置与我制作的测试项目完全相同。也许它与父母/子女关系有关,但我不确定。非常感谢任何指导。
答案 0 :(得分:0)
该行不应该是:
// self.view.addSubview(self.pageViewController.view)
通过此注释,您实际上永远不会将UIPageViewController
添加到屏幕上。