zoukankan      html  css  js  c++  java
  • addChildViewController的一个简单跳转展示子控制器

    使用addChildViewController可以有效节约内存,且可以方便的展示自己想展示的子控制器;下面是用swift一个简单实现跳转的过程。

    class first: UIViewController {

        //twothreefour分别为三个控制器

        var two: Two?

        var three: Three?

        var four: Three?

        //child测试

        var addChildVCclick: UIButton?

        var currentC: UIViewController?

     

        override func viewDidLoad() {

            super.viewDidLoad()

            self.view.backgroundColor = UIColor.blueColor()

            self.setupSubview()

        }

        

        func setupSubview() {

            addChildVCclick = UIButton()

            self.view.addSubview(addChildVCclick!)

            addChildVCclick?.frame = CGRectMake(200, 100, 20, 20)

            addChildVCclick?.backgroundColor = UIColor.greenColor()

            addChildVCclick?.userInteractionEnabled = true

            addChildVCclick?.addTarget(self, action: "addChild", forControlEvents: .TouchDown)

        }

        //点击按钮实现子控制器的添加

        func addChild(){

            print("add child")

            two = Two()

            self.addChildViewController(two!)

            three = Three()

            self.addChildViewController(three!)

            four = Three()

            four!.view.backgroundColor = UIColor.brownColor()

            self.addChildViewController(four!)

            //只有将子控制器的view添加,才会展示出子控制器

            self.view.addSubview(four!.view)

            if let f = four {

                //在第四个控制器中添加按钮,点击实现子控制器切换

                addChildVCclick?.frame = CGRectMake(200, 120, 20, 20)

                addChildVCclick?.backgroundColor = UIColor.blueColor()

                addChildVCclick?.userInteractionEnabled = true

                addChildVCclick?.addTarget(self, action: "changeViewController", forControlEvents: .TouchDown)

                f.view.addSubview(addChildVCclick!)

                currentC = f  //必须设置currenC(全局属性)

            }

        }

        func changeViewController() {

            print("changeViewController")

            two?.view.backgroundColor = UIColor.redColor()

            self.transitionFromViewController(currentC!, toViewController: two!, duration: 1, options: .TransitionCurlUp, animations: { () -> Void in

                print("animations")

                }) { (com) -> Void in

                    print("com")

                    self.currentC = self.two//在这里真正实现跳转,简单方便

                    //跳转效果由options参数决定

            }

        }

    }

  • 相关阅读:
    Ionic在Generating ES5 bundles for differential loadind的时候报错
    将整个网站变为黑白 CSS3 filter grayscale
    ionic4 sqlite 的 executeSql 方法第二个参数不传会报错
    ionic4 执行ionic cordova run android 时报错Could not find plugin "proposal-numeric-separator". Ensure there is an entry in ./available-plugins.js for it.
    ion-picker组件示例(ionic4),这个组件有样式错乱的问题
    Linux下常用命令
    搜索引擎使用技巧
    Flex布局介绍
    0浏览器内幕探寻--源头
    Geolocation API
  • 原文地址:https://www.cnblogs.com/hazhede/p/5462522.html
Copyright © 2011-2022 走看看