PageViewController iOS Swift的问题

时间:2015-02-03 19:16:09

标签: ios swift uipageviewcontroller

我在iOS中使用Swift的UIpageviewController有问题,这是代码:

class RootPageController: UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate{


     var showPhotoController: PageController! // it's the first page
     var array : NSMutableArray?
     var id : String?
     var photosActivities : NSArray? // photos that I have to show in the pages
     var index : Int?

    override func viewDidLoad() {


        self.delegate = self
        self.dataSource = self
        self.showPhotoController = self.storyboard?.instantiateViewControllerWithIdentifier("first") as PageController

        self.array = NSMutableArray()
        self.array?.addObject(self.showPhotoController)
        self.index = 0

        self.setViewControllers(self.array, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

        var model = ModelDataServer(username: "", password: "")

        self.photosActivities = model.getPhotosActivityWithID(self.id!)

        self.showPhotoController.photo.image = self.photosActivities?.objectAtIndex(self.index!) as? UIImage


        self.index = self.index! + 1

    }
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
    {

        if(self.index > 0){

            self.index = self.index! - 1

            var previousPage =  self.storyboard?.instantiateViewControllerWithIdentifier("first") as PageController


            var image: UIImage = self.photosActivities?.objectAtIndex(self.index!) as UIImage 
            previousPage.photo.image =  image
            return previousPage
       }


        return nil
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
    {



        if(self.index! < self.photosActivities!.count ){

            var newPage : PageController = self.storyboard?.instantiateViewControllerWithIdentifier("first") as PageController

        var image: UIImage = self.photosActivities?.objectAtIndex(self.index!) as UIImage 
        newPage.photo.image =  image
        self.index = self.index! + 1
        return newPage

        }
        return nil
    }

}

PageController Class

        class PageController: UIViewController {

            @IBOutlet var photo: UIImageView! = UIImageView()

            required init(coder aDecoder: NSCoder) {
                    super.init(coder: aDecoder)
                }
        }

我有一个PageViewController,它由各种页面组成。这个页面是一个视图控制器,它有一个用于显示图像的UIIMageView。这个viewcontroller的类型是PageController。

我的问题是:

当我更改页面并转到下一页时,我看到下一个PageController,但即使我在viewControllerAfterViewController中设置了新图像,我也看不到UIIMageView中的图像。

非常感谢你的时间

1 个答案:

答案 0 :(得分:0)

我认为这是因为视图控制器的视图是懒惰地实例化的,即使视图控制器本身是从故事板实例化的。结果是,当您尝试设置图像时,previousPage.photonewPage.photo都为零。为了解决这个问题,我将向PageController类添加一个单独的UIImage存储属性。

var photoToShow : UIImage = UIImage()

然后在您的viewControllerBeforeViewControllerviewControllerAfterViewController方法中,根据照片数组中的相关项设置此新属性:

newPage.photoToShow = self.photosActivities?.objectAtIndex(self.index!) as UIImage

previousPage.photoToShow = self.photosActivities?.objectAtIndex(self.index!) as UIImage

然后在viewDidLoad类的PageController中(此时视图和子视图,包括photo UIImageView)将被实例化,将photo.image设置为新存储的属性:

photo.image = photoToShow