在处理Container VC时,无法使我的Delegate协议正常工作吗?

时间:2017-08-10 15:50:17

标签: ios swift delegates

我无法让我的委托协议工作。我使用这个堆栈溢出问题作为指南dispatch event to parent ViewController in swift。我不知道自从这篇文章以来Swift 3中的内容是否发生了变化,但我的parentViewController中的函数永远不会被调用。这是我的设置。

//PROTOCOL 

protocol PDPPropDetailsDelegate {
func buttonPressed(PropDetailsVC: propertyDetailsVC)

}

//儿童视图控制器

class propertyDetailsVC: UIViewController {

  var delegate: PDPPropDetailsDelegate?

  @IBAction func emailButton(_ sender: AnyObject) {
    self.delegate?.buttonPressed(PropDetailsVC: self)
}

}

在Child View Controller中调用Button。

//家长视图控制器

class ImageDetailsVC: UIViewController, PDPPropDetailsDelegate {

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "container"{

        container = segue.destination as! ContainerViewController

    }
 }

  @IBAction func segmentControlAct(_ sender: Any) {

    switch segmentControllerView.selectedIndex  {
    case 0: print("case 1")
    container!.segueIdentifierReceivedFromParent("first")

    case 1: print("case 2")
    container!.segueIdentifierReceivedFromParent("second")
     PropertyDetailsVC.delegate = self   // **WHERE I SET DELEGATE**     
        setUpPropertyDetailsUI(property: filterImages)


    default: print("default")
    }

  }

   func buttonPressed(PropDetailsVC: propertyDetailsVC) {
    print("BUTTON PRESSED")
  }
}

永远不会调用按下按钮。我认为它与代理没有正确设置有关。不完全确定为什么会这样。我的setUpPropertyDetailsUI(属性:filterImages)从该VC获取Outlets并设置它可以正常工作。我做了一个断点,当我分割到PropertyDetailsVC时调用它。有什么建议或意见吗?

 import UIKit

 open class ContainerViewController: UIViewController {
 //Manipulating container views
 fileprivate weak var viewController : UIViewController!
//Keeping track of containerViews
fileprivate var containerViewObjects = Dictionary<String,UIViewController>()

/** Specifies which ever container view is on the front */
open var currentViewController : UIViewController{
    get {
        return self.viewController

    }
}


fileprivate var segueIdentifier : String!

/*Identifier For First Container SubView*/
@IBInspectable internal var firstLinkedSubView : String!


override open func viewDidLoad() {
    super.viewDidLoad()



}
open override func viewDidAppear(_ animated: Bool) {
    if let identifier = firstLinkedSubView{
        segueIdentifierReceivedFromParent(identifier)
    }
}
override open func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func segueIdentifierReceivedFromParent(_ identifier: String){



    self.segueIdentifier = identifier
    self.performSegue(withIdentifier: self.segueIdentifier, sender: nil)



}




override open func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == segueIdentifier{


        //Remove Container View
        if viewController != nil{


            viewController.view.removeFromSuperview()
            viewController = nil



        }
        //Add to dictionary if isn't already there
        if ((self.containerViewObjects[self.segueIdentifier] == nil)){
            viewController = segue.destination
            self.containerViewObjects[self.segueIdentifier] = viewController

        }else{
            for (key, value) in self.containerViewObjects{

                if key == self.segueIdentifier{

                    viewController = value


                }

            }


        }

        self.addChildViewController(viewController)
        viewController.view.frame = CGRect(x: 0,y: 0, width: self.view.frame.width,height: self.view.frame.height)
        self.view.addSubview(viewController.view)
        viewController.didMove(toParentViewController: self)


    }

}


}


import UIKit

class EmptySegue: UIStoryboardSegue{

override func perform() {

}

/*
 // MARK: - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
 // Get the new view controller using segue.destinationViewController.
 // Pass the selected object to the new view controller.
 }
 */

 }

1 个答案:

答案 0 :(得分:1)

你似乎对应用程序的流程感到有些困惑。以下是我为其他人写的关于同一主题的问题的答案:

https://stackoverflow.com/a/45312362/3832646

你的协议和子视图控制器看起来很棒,但是这里的其余代码有很多不妥之处:

您的prepare(for segue:_, sender:_)通常是您为目标(子)视图控制器设置委托的位置。

PropertyDetailsVC.delegate = self无法做任何事情 - 您需要一个视图控制器实例来设置其委托。

看起来您正在使用某种容器全局变量,我不确定它的用途。

看一下我发布的答案并再次开始。它在Swift 3中。