如何在UIPageViewController中快速运行到下一个和上一个按钮功能

时间:2018-07-21 05:04:10

标签: ios swift uipageviewcontroller

我使用过UIPageViewController,下面是我的代码。

class HomeViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    private var pageViewModel :PageViewModel!

    init(transitionStyle style: UIPageViewControllerTransitionStyle, navigationOrientation: UIPageViewControllerNavigationOrientation, options: [String : Any]? = nil, withViewModel viewModel:PageViewModel) {
        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: options)

        pageViewModel = viewModel
        print(pageViewModel.datasourceModel.dataListArray)

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

   l!

    var pageControl = UIPageControl()

    override func viewDidLoad() {
        super.viewDidLoad()


        self.dataSource = self
        self.delegate = self
        createPageViewController()


        self.pageControl.pageIndicatorTintColor = UIColor(red: 0.74, green: 0.69, blue: 0.78, alpha:1.0)
        self.pageControl.currentPageIndicatorTintColor = UIColor(red: 0.62, green: 0.31, blue: 0.82, alpha:1.0)
        self.pageControl.backgroundColor = UIColor(red: 0.93, green: 0.86, blue: 1, alpha:1.0)
        self.pageControl.numberOfPages = pageViewModel.datasourceModel.dataListArray! .count

        print(self.pageControl.numberOfPages)
        self.view.addSubview(pageControl)

    }

    fileprivate func createPageViewController() {

        if (pageViewModel.datasourceModel.dataListArray?.count)! > 0 {
            let firstController = getItemController(0)!
           let startingViewControllers = [firstController]


            self.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
        }

    }

    fileprivate func setupPageControl() {



    }

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

        let itemController = viewController as! HomeController

        if itemController.itemIndex > 0 {
            return getItemController(itemController.itemIndex-1)
        }

        return nil
    }

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


        print(pageViewController)


        let itemController = viewController as! HomeController

        if itemController.itemIndex+1 < (pageViewModel.datasourceModel.dataListArray?.count)! {
            return getItemController(itemController.itemIndex+1)
        }



        return nil
    }
//fileprivate

  func getItemController(_ itemIndex: Int) -> HomeController? {

        if itemIndex < (pageViewModel.datasourceModel.dataListArray?.count)! {

                          let homeViewModel :HomeViewModel = HomeViewModel(withmodel:pageViewModel.datafordisplay(objectatindex: itemIndex))
                let ViewController = HomeController(nibName: "HomeController", bundle: nil, withViewModel: homeViewModel)
            ViewController.itemIndex = itemIndex
            return ViewController
        }

        return nil
    }



    func getItemController1(_ itemIndex: Int) {

        if itemIndex < (pageViewModel.datasourceModel.dataListArray?.count)! {

            let homeViewModel :HomeViewModel = HomeViewModel(withmodel:pageViewModel.datafordisplay(objectatindex: itemIndex))
            let ViewController = HomeController(nibName: "HomeController", bundle: nil, withViewModel: homeViewModel)
            ViewController.itemindex1 = itemIndex
            print(ViewController.itemindex1)


      //      pageViewController(homeViewModel, viewControllerAfter: ViewController.itemindex1)

        }


    }


    func currentControllerIndex() -> Int {


        let pageItemController = self.currentController()

        if let controller = pageItemController as? HomeController{


            print(controller.itemIndex)

            return controller.itemIndex
        }

        return -1
    }

    func currentController() -> UIViewController? {



        if (self.viewControllers?.count)! > 0 {
            return self.viewControllers![0]
        }

        return nil
    }


    func pageViewController(
        _ pageViewController: UIPageViewController,
        willTransitionTo pendingViewControllers:[UIViewController]){
        if let itemController = pendingViewControllers[0] as? HomeController {
            self.pageControl.currentPage = itemController.itemIndex
        }
    }

}

和HomeController如下:-

class HomeController: UIViewController ,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate{

    var itemIndex: Int = 0
    var i = 0
    var j = 0
    var k = 0


    var itemindex1 :  Int = 0

    @IBOutlet weak var name: UITextField!






    private var homeViewModel :HomeViewModel!


    @IBOutlet weak var tableview1: UITableView!

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, withViewModel viewModel:HomeViewModel) {

        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

        homeViewModel  = viewModel
    }


    @IBOutlet weak var screen3: UIView!

    @IBOutlet weak var screen1: UIView!

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()




           name.delegate = self





        if itemIndex == 0{
            screen1.isHidden = false
            tableview1.isHidden = true
            screen3.isHidden = true

        }



        else if itemIndex == 1{
            screen1.isHidden = true
            tableview1.isHidden = false
            screen3.isHidden = true


        }

        else if itemIndex == 2{

               screen1.isHidden = true
               tableview1.isHidden = true
               screen3.isHidden = false
               submitbutton.isHidden = false


        }



     //   self.restaurantname.text = homeViewModel.homeModel.name

              // Do any additional setup after loading the view.
    }





    func textFieldDidEndEditing(_ textField: UITextField) {


    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {


        return true;
    }

    func textFieldShouldClear(_ textField: UITextField) -> Bool {
       // tableView.reloadData()
        return true;
    }


    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {




            let names =  name.text
            self.homeViewModel.name = names

            self.homeViewModel.isValidInput(Input:self.homeViewModel.name!)


        */

             return true;
    }

    /*
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {




        */

        return true


    }
    */


    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder();


        name.resignFirstResponder()
        return true;
    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

这里我有1个UIViewController。在UIViewController内部我有

2个UIView和1个表视图。分别是screen1和screen3是UIView和一个表视图。

在这里,根据编码,我使用UIPageViewController滚动了视图。

现在我需要在UIPageViewController中的两个按钮分别作为下一个和上一个按钮。因此,当我单击下一个按钮时,我需要滚动视图并显示表格视图。再次单击下一个按钮,它应该滚动到屏幕3。做UIPageControl也应该移动。如何解决。

对于上一个按钮,功能相同但向后。该怎么办?

0 个答案:

没有答案