Pass parameter to selector function in Swift

22,213

Solution 1

set your timer with the userinfo

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

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

}

enter image description here

Solution 2

You pass in the data you want as the userInfo argument when creating the Timer:

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

and make the callback look like:

func downloadTimer(_ timer: Timer) {
    // access timer.userInfo here
}
Share:
22,213
Pippo
Author by

Pippo

Updated on February 11, 2020

Comments

  • Pippo
    Pippo over 4 years

    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)
    

    Function

    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
    
    }
    

    enter image description here

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

    enter image description here

  • Pippo
    Pippo over 7 years
    Sorry i attached this in the wrong box and it won't let me remove it
  • Harshal Valanda
    Harshal Valanda over 7 years
    casting cell as a InnerCollectionCell
  • Harshal Valanda
    Harshal Valanda over 7 years
    let cell = timer.userInfo as InnerCollectionCell
  • Pippo
    Pippo over 7 years
    That makes sense. Thanks
  • famfamfam
    famfamfam over 4 years
    hi, i can stop timer by cancelPreviousPerformRequests, plz help