如何在swift中实现下面的代码

时间:2017-08-30 06:55:26

标签: ios swift

我希望页面视图控制器在我的家庭视图控制器的中间,所以我使用容器视图和嵌入到页面视图控制器已成功在目标c现在我想使用相同的程序为swift可以任何人帮助我如何在swift中实现代码

    _pageImages = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpeg",@"5.jpg"];
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;

直到上面我已经改为swift但是代码无法改变

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

我的转换器代码

 arrPageTitles=["blue.jpg","blue.jpg","blue.jpg"]
  //  let pageViewController = UIPageViewController()
    self.pageViewController=self.storyboard?.instantiateViewController(withIdentifier: "pageViewController") as! UIPageViewController
    self.pageViewController.delegate=self
    self.pageViewController.dataSource=self
    var startingViewController = PageContentViewController()
    startingViewController=self.getViewControllerAtIndex(0)
    let viewcontrollers = NSArray()=startingViewController
self.pageViewController.setViewControllers(viewcontrollers, direction:       UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)

但是收到错误

let viewcontrollers = NSArray()=startingViewController
此方法下面的

也不会将字符串传输到setphotoname

 func getViewControllerAtIndex(_ index: NSInteger) -> PageContentViewController
{
    // Create a new view controller and pass suitable data.
    var pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController

    pageContentViewController.setPhotoName = arrPageTitles[index] as! String
   // pageContentViewController.strPhotoName = "\(arrPageTitles[index])"
    pageContentViewController.pageIndex = index

    return pageContentViewController
}

3 个答案:

答案 0 :(得分:0)

尝试查看 Swift syntax

创建instance之类的:

var startingViewController:  YOUR_startingViewController! //You can access this globally if you have pageControl target for indicators

并在viewDidLoad中更新,如下所示:

startingViewController = self.storyboard?.instantiateViewController(withIdentifier: "startingViewController_IDENTIFIER") as! YOUR_startingViewController
 //You can use let for creating startingViewController instance if you dont want to access it globally        
//You dont need to create array just put the instance of controller directly like this:

PageViewController.setViewControllers([startingViewController],direction: .forward, animated: false,completion: nil)

答案 1 :(得分:0)

这样做,

let startingViewController: PageContentViewController? = viewController(at: 0)
let viewControllers: [UIViewController] = [startingViewController]
pageViewController.setViewControllers(viewControllers, direction: .forward, animated: false) { _ in }

答案 2 :(得分:0)

斯威夫特3:

let startingViewController: PageContentViewController? = viewController(at: 0)
self.pageViewController.setViewControllers([startingViewController], direction: .forward, animated: false, completion: nil)