本文介绍了将参数传递给Swift中的选择器函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数传递给 timer 中称为 selector 的函数.特别是对cell的引用,这样我就可以在 UI 中更新某些内容.

I would like to pass a parameter to a function called as a selector from a timer. Specifically the reference to a cell so i can update something in the UI.

所以我想要这样的东西:

So what I want to something like this:

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(cell: cell), userInfo: nil, repeats: true)

功能

func downloadTimer(cell: InnerCollectionCell) {
    cell.progressBar.setProgress(downloadProgress, animated: true)
}

尽管我可能会做些侄女,但前提是可以做到这一点?

Though I might be a bit niece in assuming this can be done?

------编辑------

------ EDIT ------

按照以下示例,但无法像往常一样从单元格中获得预期结果

As per the below examples, but not getting expected results as usual from a cell

let innerCell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(_:)), userInfo: innerCell, repeats: true)


func downloadTimer(_ timer: Timer) {

    let cell = timer.userInfo

    cell. // no options as expected of a cell

}

如果数据发送正确,我希望有更多这样的选择:

I expected more options like this if the data was sent correctly:

推荐答案

使用用户信息设置计时器

set your timer with the userinfo

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(cell: cell), userInfo: data, repeats: true)

并按照以下方式获取userinfo

and get userinfo as follow

func downloadTimer(_ timer: Timer) {
   let data = timer.userInfo
}

------编辑------

------ EDIT ------

按照以下示例,但无法像往常一样从单元格中获得预期结果

As per the below examples, but not getting expected results as usual from a cell

let innerCell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(_:)), userInfo: innerCell, repeats: true)


func downloadTimer(_ timer: Timer) {

    let cell = timer.userInfo as! InnerCollectionCell

    cell. // no options as expected of a cell

}

这篇关于将参数传递给Swift中的选择器函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:01