简体   繁体   English

如何在swift中的nib类之间使用协议委托?

[英]how to use protocol delegate between nib class in swift?

i have one view controller that it has two sub view of nib files.我有一个视图控制器,它有两个 nib 文件的子视图。 my first nib name is citycollection and secend is currentxib that both of them are UIVIEW.我的第一个笔尖名称是 citycollection,而 secend 是 currentxib,它们都是 UIVIEW。 in citycollection view i have collectionview that i want when i click on item of that, print the data i send by protocol in the label of currentxib class.( attention both of them are UIView and not view controller.) but it not work.在 citycollection 视图中,当我点击其中的项目时,我有我想要的 collectionview,在 currentxib 类的标签中打印我通过协议发送的数据。(注意它们都是 UIView 而不是视图控制器。)但它不起作用。 my main view controller class is empty still.我的主视图控制器类仍然是空的。 this is my code:这是我的代码:

CityCollection class: 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 class : 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
    }
}

what should i do?我该怎么办?

The problem is here:问题在这里:

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

All you do here is create a new instance of the CityCollection class.您在此处所做的就是创建 CityCollection 类的新实例 You set that instance's delegate in the next line.您在下一行设置该实例的委托。 And then... myCity , your CityCollection object, vanishes in a puff of smoke.然后... myCity ,您的 CityCollection 对象,消失在一阵烟雾中。 So those two lines are useless.所以这两行没用。

What you probably meant to do was to obtain, somehow, a reference to an aleady existing CityCollection object that is present somewhere else in your interface.您可能想要做的是以某种方式获取对存在于界面中其他位置的现有CityCollection 对象的引用。

From ViewController, which is now empty, assuming ParentViewController .从 ViewController,它现在是空的,假设ParentViewController In this view controller , you are holding two nibs在这个视图控制器中,你拿着两个笔尖

In viewDidLoad of the parent view controller, you add your collectionView.delegate = self .在父视图控制器的 viewDidLoad 中,添加collectionView.delegate = self and implement your delegate method并实现您的委托方法

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

So far, you have a parentViewController , with two views, one of collection view, didSelect in one item, and parentviewController knows which name is selected.到目前为止,你有一个 parentViewController ,有两个视图,一个集合视图,一个项目中的 didSelect,并且 parentviewController 知道选择了哪个名称。 Now you have to send Data to CurrentXib.现在您必须将数据发送到 CurrentXib。

To do so, you may do notification为此,您可以执行通知

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) 
     }
}

you have to listen the notification, add lines awakefromNib of CurrentXIB,你必须听通知,添加行唤醒从 CurrentXIB 的 Nib,

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

now you have to add this metods现在你必须添加这个方法

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
        }
    }
}

i found the solution .我找到了解决方案。

first i clean below code from awakeFromNib :首先我从awakeFromNib 中清除以下代码:

 let myCity = CityCollection() 
    myCity.delegate = self

then i add outlets of subviews in my main viewcontroller class:然后我在我的主视图控制器类中添加子视图的出口:

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

then i write below code in viewDidLoad:然后我在 viewDidLoad 中编写以下代码:

self.cityCollection.delegate = self.currentXib

you can take share instance of CityCollection in CityCollection.class like this您可以像这样在 CityCollection.class 中共享CityCollection实例

static let shared = CityCollection()

After that in your CurrentXib you can use with this之后在你的CurrentXib你可以使用这个

CityCollection.shared.delegate = self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM