如何使用mvvm

时间:2018-07-24 06:28:44

标签: ios mvvm

我需要实现类似于此链接的功能。 This is the way i need.

根据此链接,它具有1个ViewController和PageViewController,并且很快完成了。

但是对我来说我有3个ViewController。而且我正在使用swift在没有主脚本的情况下在mvvm中进行开发。因此我需要根据此链接进行开发,但它不应该使用主脚本,而必须使用3个ViewController.So单击前进按钮时,应显示下一个ViewController。类似于单击反向按钮时,应显示前一个ViewController。

我尝试一些示例,但没有得到输出。

我已经有UIPageViewController。

我的代码如下。

型号:-

class HomeModel: NSObject {

    var name:String!


    init?(dictionary :JSONDictionary) {

        guard
            let name = dictionary["name"] as? String


            else {
                return

        }

        self.name = name


}


}

HomeDataSourceModel:-

class HomeDataSourceModel: NSObject {

    var dataListArray:Array<HomeModel>? = []


    init(array :Array<[String:Any]>?) {
        super.init()
        var newArray:Array<[String:Any]> = []
        if array == nil{

            newArray = self.getJsonDataStored()
            print(newArray.count)

        }
        else{
            newArray = array!

        }

        var datalist:Array<HomeModel> = []
        for dict in newArray{

            let model = HomeModel(dictionary: dict)

            datalist.append(model!)
        }
        self.dataListArray = datalist
    }

}

typealias dummyDataSource = HomeDataSourceModel
extension dummyDataSource{

    func getJsonDataStored() ->Array<Dictionary<String,String>>{


        let jsonArray = [["name":""],["name":""],["name":""]]as Array<Dictionary<String,String>>



        return jsonArray
    }

}

pageviewmodel:-

class PageViewModel: NSObject {

    var datasourceModel: HomeDataSourceModel

    var index:Int?



    init(withdatasource  newDatasourceModel:HomeDataSourceModel){
        datasourceModel = newDatasourceModel
        print(datasourceModel.dataListArray?.count)
    }



   func datafordisplay(objectatindex index: Int) -> HomeModel{
      return  (datasourceModel.dataListArray?[index])!
    }




}

pageviewcontroller:-

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")
    }



    var pageControl = UIPageControl()

    override func viewDidLoad() {
        super.viewDidLoad()


        self.navigationItem.leftBarButtonItem = nil
        self.view.backgroundColor = UIColor(red: 0.93, green: 0.86, blue: 1, alpha:1.0)
        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
        }
    }

    func scrollToNextViewController() {
        print(viewControllers?.first)

        if let visibleViewController = viewControllers?.first,
            let nextViewController =
            pageViewController(self, viewControllerAfter: visibleViewController) {


            print(nextViewController)
           scrollToViewController(nextViewController)
        }
    }




}

homeviewcontroller:-

class HomeController: UIViewController ,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate{

    var itemIndex: Int = 0

    var itemindex1 :  Int = 0

    @IBOutlet weak var submitbutton: UIButton!
    @IBOutlet weak var name: UITextField!

    @IBOutlet weak var nextbutton: UIButton!


    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
    }




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


    override func viewDidLoad() {
        super.viewDidLoad()

           name.delegate = self
            // Do any additional setup after loading the view.
    }


    @IBAction func nextbutton(_ sender: AnyObject) {

    }
       @IBAction func submitbutton(_ sender: AnyObject) {

    }

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

}

那么如何使用3 UIViewController在上述链接中实现mvvm?

0 个答案:

没有答案