如何在笔尖类之间快速使用协议委托?

时间:2019-02-02 04:26:51

标签: ios swift

我有一个视图控制器,它有两个nib文件子视图。 我的第一个笔尖名字是citycollection,而secend是currentxib,它们都是UIVIEW。 在citycollection视图中,当我单击该项目时,我想要一个collectionview,在currentxib类的标签中打印通过协议发送的数据(注意它们都是UIView而不是view controller。),但它不起作用。 我的主视图控制器类仍然为空。 这是我的代码:

CityCollection类:

///// CityCollection

class CityCollection: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    weak var delegate: sendDataDelegate?

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let fName = Publics.instance.getCities()[indexPath.row].fName

        delegate?.name(data: fName)

    }

}



protocol sendDataDelegate : NSObjectProtocol {
    func name(data : String)
}

CurrentXib类:

////CurrentXib
class CurrentXib: UIView, sendDataDelegate {


    func name(data: String) {
        lblCityName.text = data
    }


    @IBOutlet weak public var lblCityName: UILabel!



    override func awakeFromNib() {

        let myCity = CityCollection()
        myCity.delegate = self
    }
}

我该怎么办?

4 个答案:

答案 0 :(得分:1)

问题在这里:

    let myCity = CityCollection() // <-- this is the problem
    myCity.delegate = self

您在这里要做的就是创建CityCollection类的新实例。在下一行中设置该实例的委托。然后... myCity(您的CityCollection对象)在浓烟中消失。所以这两行是没用的。

您可能想要做的是,以某种方式获得对存在于界面中其他位置的 aleady现有 CityCollection对象的引用。

答案 1 :(得分:0)

从ViewController(假定为ParentViewController,现在为空)。在此视图控制器中,您持有两个笔尖

在父视图控制器的viewDidLoad中,添加collectionView.delegate = self。并实现您的委托方法

extension ParentViewController : sendDataDelegate {
     func name(data : String) {
        print(data) // you have got your data here
     }
}

到目前为止,您有一个parentViewController,具有两个视图,其中一个视图是集合视图,在一项中是didSelect,并且parentviewController知道选择了哪个名称。现在,您必须将数据发送到CurrentXib。

为此,您可以进行通知

extension ParentViewController : sendDataDelegate {
     func name(data : String) {
        print(data) // you have got your data here
        let dataDict = ["name" : data]
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo:dataDict) 
     }
}

您必须收听通知,在CurrentXIB的abakefromNib中添加行,

 NotificationCenter.default.addObserver(self, selector: #selector(self.method(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

现在您必须添加此方法

func method(_ notification: NSNotification) {
    if let dict = notification.userInfo as? [String : String] {
        if let name = dict["name"] as? String{
            //currentXIB got the name right now, he has to update now
              lblCityName.text = name
        }
    }
}

答案 2 :(得分:0)

我找到了解决方案。

首先我从awakeFromNib清除以下代码:

 let myCity = CityCollection() 
    myCity.delegate = self

然后在主viewcontroller类中添加子视图的出口:

  @IBOutlet weak var cityCollection: CityCollection!
    @IBOutlet weak var currentXib: CurrentXib!

然后我在viewDidLoad中编写以下代码:

self.cityCollection.delegate = self.currentXib

答案 3 :(得分:-1)

您可以像这样在CityCollection.class中获取F1 F2 1 2 1 3 4 1 的共享实例

CityCollection

之后,您可以在static let shared = CityCollection() 中使用它

CurrentXib