将参数传递给Swift中的选择器功能

Pip*_*ppo 4 parameters timer selector ios swift

我想将一个参数传递给一个从计时器调用为选择器的函数.具体来说,我可以在UI中更新内容.cell

那么我想要的是这样的:

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

功能

func downloadTimer(cell: InnerCollectionCell) {
    cell.progressBar.setProgress(downloadProgress, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

虽然我可能会有点侄女假设这可以做到吗?

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

根据以下示例,但没有像往常一样从单元格获得预期结果

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

}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果数据发送正确,我期待更多这样的选项:

在此输入图像描述

Har*_*nda 11

使用userinfo设置您的计时器

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

并获取userinfo如下

func downloadTimer(_ timer: Timer) {
   let data = timer.userInfo
}
Run Code Online (Sandbox Code Playgroud)

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

根据以下示例,但没有像往常一样从单元格获得预期结果

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

}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


sim*_*ere 9

在创建Timer时,您将所需的数据作为userInfo参数传递:

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(_:), userInfo: myData, repeats: true)
Run Code Online (Sandbox Code Playgroud)

并使回调看起来像:

func downloadTimer(_ timer: Timer) {
    // access timer.userInfo here
}
Run Code Online (Sandbox Code Playgroud)