优化代码:遍历UIViewcontroller的子类

时间:2017-04-11 09:57:41

标签: ios iphone swift loops

我对swift很新,所以我对我尝试创建的代码有一个基本的问题。

我有UIScrollView,我想添加多个UIViewController

这是我首先使用的代码:

let obj1 = self.storyboard?.instantiateViewController(withIdentifier: "Page1VC") as! Page1VC
        let obj2 = self.storyboard?.instantiateViewController(withIdentifier: "Page2VC") as! Page2VC
        let obj3 = self.storyboard?.instantiateViewController(withIdentifier: "Page3VC") as! Page3VC
        let obj4 = self.storyboard?.instantiateViewController(withIdentifier: "Page4VC") as! Page4VC
        let obj5 = self.storyboard?.instantiateViewController(withIdentifier: "Page5VC") as! Page5VC

        self.containerScrollView.addSubview(obj2.view)
        obj2.willMove(toParentViewController: self)
        self.addChildViewController(obj2)

        self.containerScrollView.addSubview(obj1.view)
        obj1.willMove(toParentViewController: self)
        self.addChildViewController(obj1)

        self.containerScrollView.addSubview(obj3.view)
        obj3.willMove(toParentViewController: self)
        self.addChildViewController(obj3)

        self.containerScrollView.addSubview(obj4.view)
        obj4.willMove(toParentViewController: self)
        self.addChildViewController(obj4)

        self.containerScrollView.addSubview(obj5.view)
        obj5.willMove(toParentViewController: self)
        self.addChildViewController(obj5)

        obj1.view.frame.origin =  CGPoint.init(x: 0, y: 0)
        obj2.view.frame.origin =  CGPoint.init(x: UIScreen.main.bounds.width, y: 0)
        obj3.view.frame.origin = CGPoint.init(x: 2*UIScreen.main.bounds.width, y: 0)
        obj4.view.frame.origin = CGPoint.init(x: 3*UIScreen.main.bounds.width, y: 0)
        obj5.view.frame.origin = CGPoint.init(x: 4*UIScreen.main.bounds.width, y: 0)

我希望大家都同意,这段代码不是很好和/或灵活。我想为UIScrollView获得任意数量的页面,所以我得出结论,我需要一个Loop用于此目的。

我最终得到了这段代码:

repeat {
            let vc = (self.storyboard?.instantiateViewController(withIdentifier: "Page\(x+1)VC"))!
            obj.append(vc)
            self.containerScrollView.addSubview(obj[x].view)
            obj[x].willMove(toParentViewController: self)
            self.addChildViewController(obj[x])
            obj[x].view.frame.origin =  CGPoint(x: (CGFloat(x))*UIScreen.main.bounds.width, y: 0)
            x += 1
            print("Page \(x) sucessfully initialized")
        } while (x < segmentControl.numberOfSegments)

该代码工作正常,但我想更改childViewController中的部分数据。这让我想到了这些变化:

repeat {
            let vc = (self.storyboard?.instantiateViewController(withIdentifier: "Page\(x+1)VC"))! as! Page**\(x+1)**VC
            **vc.setup = setup**
            obj.append(vc)
            self.containerScrollView.addSubview(obj[x].view)
            obj[x].willMove(toParentViewController: self)
            self.addChildViewController(obj[x])
            obj[x].view.frame.origin =  CGPoint(x: (CGFloat(x))*UIScreen.main.bounds.width, y: 0)
            x += 1
            print("Page \(x) sucessfully initialized")
        } while (x < segmentControl.numberOfSegments)

我标记了Bold的变化。 该代码显然不起作用,因为无法将UIViewController的类型声明为as! Page**\(x+1)**VC

所有这些导致我的问题:我可以通过使用循环遍历代码的长版本中描述的不同子类吗?或者我是否必须使用我的代码的长版本?

0 个答案:

没有答案