容器视图,失败的使用委托

时间:2015-10-15 05:28:40

标签: ios swift

我想设计幻灯片功能。所以我在故事板中使用容器视图,没有任何segue.there有一个按钮在centerView打开或关闭leftView.but这个功能写在container.swift,我不知道如何设置delegate..please帮帮我,谢谢。(所有代码如下)

Container.swift

import UIKit

class ContainerViewController: UIViewController,test {


    var leftViewController: UIViewController? {
        willSet{
            if self.leftViewController != nil {
                if self.leftViewController!.view != nil {
                    self.leftViewController!.view!.removeFromSuperview()
                }
                self.leftViewController!.removeFromParentViewController()
            }
        }

        didSet{

            self.view!.addSubview(self.leftViewController!.view)
            self.addChildViewController(self.leftViewController!)
        }
    }

    var rightViewController: UIViewController? {
        willSet {
            if self.rightViewController != nil {
                if self.rightViewController!.view != nil {
                    self.rightViewController!.view!.removeFromSuperview()
                }
                self.rightViewController!.removeFromParentViewController()
            }
        }

        didSet{

            self.view!.addSubview(self.rightViewController!.view)
            self.addChildViewController(self.rightViewController!)
        }
    }

    var menuShown: Bool = false


    func showMenu() {

        print(__FUNCTION__,__LINE__)
        UIView.animateWithDuration(0.3, animations: {
            self.rightViewController!.view.frame = CGRect(x: self.view.frame.origin.x + 235, y: self.view.frame.origin.y, width: self.view.frame.width, height: self.view.frame.height)
            }, completion: { (Bool) -> Void in
                self.menuShown = true
        })
    }

    func hideMenu() {
        UIView.animateWithDuration(0.3, animations: {
            self.rightViewController!.view.frame = CGRect(x: 0, y: self.view.frame.origin.y, width: self.view.frame.width, height: self.view.frame.height)
            }, completion: { (Bool) -> Void in
                self.menuShown = false
        })
    }

    var centerView:CenterViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let CenterNC:UINavigationController = storyboard.instantiateViewControllerWithIdentifier("CenterNC") as! UINavigationController

        let leftVC:LeftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftVC") as! LeftViewController

         centerView = storyboard.instantiateViewControllerWithIdentifier("CenterVC") as! CenterViewController

        self.leftViewController = leftVC
        self.rightViewController = CenterNC


       self.centerView.delegate = self


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


import UIKit

protocol test{
    func showMenu()
    func hideMenu()
}

class CenterViewController: UIViewController {

    var delegate:test! = nil


    @IBAction func pressed(sender: AnyObject) {

        if (delegate != nil) {
            delegate.showMenu()
        }
        else{
            print("Error")
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

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

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

Debug ContainerView.swift

Debug CenterViewController.swift

1 个答案:

答案 0 :(得分:0)

通常你使用whatever.delegate = self,但我不太确定你想做什么。 (还whatever.datasource = self)。我不知道这是否适用于VC。

据我所知,在包含其他两个VC的故事板中有一个UIViewController是不好的做法。

相反,您应该以编程方式或使用故事板segue进行转换。

self.presentViewController(LeftViewController(), animated: true, completion: nil) self.dismissViewController(true, completion: nil)

视图控制器会自动从内存中删除(如果您好奇的话,请查看ARC)(只要您没有要删除的VC中的变量,这些变量是从另一个未被取消初始化的类中访问的(或可以) - 如果这令人困惑,请忽略它。)

如果你想要一个滑动动画或类似的东西,最好在GitHub上找到一些东西来克隆并导入到你的项目中。我创建了一个带有标签栏的应用程序,该标签栏可以在视图之间滑动,并在网上找到一些代码。

我一直在哼哼,但是在一个VC中有两个VC对于记忆和一般的不良做法都很糟糕。改为使用自定义分段。