0
votes

I learn to use the UIPageViewController in swift from this tutorial In the main View Controller:


// MARK: - Variables
private var pageViewController: UIPageViewController?

// MARK: - View Lifecycle
override func viewDidLoad() {
        super.viewDidLoad()
        createPageViewController()
        setupPageControl()
}

private func createPageViewController() {

        let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
        pageController.dataSource = self

        if contentImages.count > 0 {
            let firstController = getItemController(0)!
            let startingViewControllers: NSArray = [firstController]
            pageController.setViewControllers(startingViewControllers as! [PageItemController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
        }

        pageViewController = pageController
        addChildViewController(pageViewController!)
        self.view.addSubview(pageViewController!.view)
        pageViewController!.didMoveToParentViewController(self)
    }

Can anyone explain for me, why we don't assign the pageViewController at the beginning like:

pageViewController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController

Why do we need to assign the value to the "let" variable and then assign the "let" variable to the "var" variable?

Why do we make the thing complicated? :)

1
self.storyboard is probably not immediately available (before viewDidLoad).Thilo
Maybe for scopping reasons. If it was declared inside the function, it wouldn't be available outside.Carcigenicate
Have you tried this your approach? Does it makes any difference?Yogesh Suthar
With my approach, I got this warning: " Treating a forced downcast to 'UIPageViewController' as optional will never produce 'nil' "chipbk10
because we don't want to overwhelmed the initializing procedure of the class; and we need to consider to being a good memory users (as that resource is very precious and limited!), and the main principal is we load a part into a memory when it becomes necessary. the init procedure should not load any extra thing which is not sure to be used in the classes lifecycle, because after initing the class can be released immediately, and it would be pointless to load a massive amount of data which will be used only after the view loads, just for releasing it in a very next iteration.holex

1 Answers

6
votes

Your way is absolutely possible. Note there is never only one correct way to code. However, you have overlooked one side effect - instantiateViewControllerWithIdentifier returns an optional with undefined type (AnyObject!) so we have to cast it to a specific type UIPageViewController. The side effect is that the programmer has decided to also remove the optional during that cast, using as! (result is UIPageViewController not UIPageViewController!).

In general it's useful to remove optionals in the beginning of every function and then use non-optionals. Since the instance variable pageViewController is an optional, it would be harder and less readable using it because you would have to check for nil (or force unwrap) for every use.

Note the difference in your code:

pageController.setViewControllers(...
...
pageViewController!.didMoveToParentViewController(...

In my opinion the code in your example is not very good because it combines both approaches freely. Using either

pageViewController!.setViewControllers(...
...
pageViewController!.didMoveToParentViewController(...

or

pageController.setViewControllers(...
...
pageController.didMoveToParentViewController(...

would be more readable.