Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating a view controller with identifier in Swift

Normally I would've done the following to create a view controller and set its dataSource using Objective-C:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create the data model
    _pageTitles = @[@"Over 200 Tips and Tricks", @"Discover Hidden Features", @"Bookmark Favorite Tip", @"Free Regular Update"];
    _pageImages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];

    // Create page view controller
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;

    PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    // Change the size of page view controller
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);

    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];

}

Now I would like to convert it to Swift. This is my attempt so far:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    // Create page view controller
    self.pageViewController = self.storyboard.instantiateViewControllerWithIdentifier("PageViewController") as UIPageViewController
    self.pageViewController.dataSource = self;

    var startingViewController = PageContentViewController(nibName: nil, bundle: nil)
    startingViewController = self.viewControllerAtIndex(0)
    var viewControllers: NSArray = [startingViewController];
    self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection, animated: 0, completion: nil)

    // Change the size of page view controller
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);

    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)
}

I'm not sure why it's throwing an error for:

self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection, animated: 0, completion: nil)

Error: Expected member name or constructor call after type name.

like image 342
fulvio Avatar asked Mar 20 '23 10:03

fulvio


1 Answers

The type of the direction: parameter is UIPageViewControllerNavigationDirection, therefore the argument must be one of the possible values, e.g. UIPageViewControllerNavigationDirection.Forward (which can be shortened to .Forward due to the automatic type inference).

And the type of the animated: parameter is Bool, therefore the argument must be true or false, and not 0.

Together:

self.pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: false, completion: nil)
like image 175
Martin R Avatar answered Mar 22 '23 11:03

Martin R