代码之家  ›  专栏  ›  技术社区  ›  richc

调用嵌入式containerVC方法时未调用父VC委托方法

  •  0
  • richc  · 技术社区  · 8 年前

    我有一个父VC,子VC嵌入到容器中。两个VCs都符合委托,但只调用子委托方法。如何让VC的两个委托方法都响应?我是否缺少容器视图的委托模式?提前感谢您的帮助。

    中心类:

    public protocol BLEManagerDelegate: class {
       func bLEManagerShowAlert(message: String)
    }
    
    public class BLEManager: NSObject {
    
      static let sharedInstance = BLEManager()
      weak var delegate: BLEManagerDelegate?
    
       public func postMessage() {
          delegate?.bLEManagerShowAlert(message: message)
       }
    }
    

    家长VC

    class HomeVC: ContentViewController, BLEManagerDelegate {
    
        var bLEManager = BLEManager.sharedInstance 
    
        override func viewWillAppear(_ animated: Bool) {
            bLEManager.delegate = self
        }
    
        // delegate methods
    
        func bLEManagerShowAlert(message: String) {
        // THIS METHOD IS NOT GETTING CALLED
        }
    }
    

    嵌入到ParentVC中的容器视图

    class ChildVC: UITableViewController, BLEManagerDelegate {
    
        var bLEManager = BLEManager.sharedInstance 
    
        override func viewWillAppear(_ animated: Bool) {
            bLEManager.delegate = self
    
        // delegate methods
    
        func bLEManagerShowAlert(message: String) {
        // This method IS getting called
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Paulw11    8 年前

    你的 delegate 属性一次只能保存对一个对象的引用。一旦你 ChildVC 将自身设置为委托 parentVC 不再是代理。

    如果要通知多个对象,可以使用 NotificationCenter

        2
  •  1
  •   Yesbol Kulanbekov    8 年前

    为什么需要Singleton BLEManager?在哪里调用postMessage()?如果警报显示在自己的视图控制器中,只需通过协议扩展为默认警报消息编写默认实现。然后只需在VCs中实现自定义消息的方法。如果需要多个代理,应尝试以下操作: http://www.gregread.com/2016/02/23/multicast-delegates-in-swift/