【问题标题】:Page view in Container not showing content容器中的页面视图不显示内容
【发布时间】:2016-08-02 03:16:25
【问题描述】:

我正在尝试使用容器将页面视图放置到主视图上。由于我是 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 值传递给图像视图,但据我所知,它们的设置与我制作的测试项目完全相同。也许它与父母/孩子的关系有关,但我不确定。非常感谢任何指导。

【问题讨论】:

    标签: ios swift2 uipageviewcontroller uicontainerview


    【解决方案1】:

    这行不应该被注释掉:

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

    有了这个评论,您实际上永远不会将UIPageViewController 添加到屏幕上。

    【讨论】:

    • 感谢您的回复。抱歉,在我进行一些更改时无意中发布了问题之前,评论行就这样留下了。即使没有评论该行,我仍然有错误。
    • 如果您不介意看一下,我在这里就同一问题提出了一个更相关和更新的问题。 stackoverflow.com/questions/36560473/…
    • 什么错误?屏幕上什么也没有出现,或者您的图像出现错误?因为我看到了你关于图像的其他问题,问题是你初始化变量太晚了。
    • 您介意详细说明一下吗?对不起,我是新手,只是想弄清楚。我应该在哪里初始化它们?
    • 我评论了你的另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多