Passing Arguments to Selector in Swift

Passing arguments to selector in Swift

It looks like you're misunderstanding a couple of things.

When using target/action, the function signature has to have a certain form…

func doSomething()

or

func doSomething(sender: Any)

or

func doSomething(sender: Any, forEvent event: UIEvent)

where…

The sender parameter is the control object sending the action message.

In your case, the sender is the UITapGestureRecognizer

Also, #selector() should contain the func signature, and does NOT include passed parameters. So for…

func handleTap(sender: UIGestureRecognizer) {

}

you should have…

let gesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))

Assuming the func and the gesture are within a view controller, of which modelObj is a property / ivar, there's no need to pass it with the gesture recogniser, you can just refer to it in handleTap

Swift: Passing a parameter to selector

#selector describes method signature only. In your case the correct way to initialize the selector is

let selector = #selector(moveToNextTextField(tag:))

Timer has the common target-action mechanism. Target is usually self and action is a method that takes one parameter sender: Timer. You should save additional data to userInfo dictionary, and extract it from sender parameter in the method:

func moveToNextTextField(sender: Timer) {
print(sender.userInfo?["tag"])
}
...
let selector = #selector(moveToNextTextField(sender:))
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: selector, userInfo: ["tag": 2], repeats: false)

How to pass Argument to method with '#selector'

You never ever pass arguments to the method you are referring to when using the #selector literal. The argument(s) will be passed to the method by the performer of the selector, which in this case, is the inner workings of UIBarButtonItem.

To solve this, simply remove the argument list:

#selector(barClicked)

When the selector is performed, the sender parameter will most likely hold a reference to the second UIBarButtonItem in the tool bar, since you are setting the selector as the action for the second UIBarButtonItem.

However, t seems like that you deliberately want the sender to be the first UIBarButtonItem of the toolbar. This sounds quite counter-intuitive. You might be doing something wrong here. But if you insist on passing the first item of the tool bar, do this:

// add a new method like this:
func someMethod() { // name this properly!
barClicked(sender: bar.items[0]) // please make bar a class-level variable first.
}

Then you can refer to someMethod with #selector:

#selector(someMethod)

Pass function argument into selector in Swift

You can find the answer in your question :)
Simply use Selector parameter type, and no need #selector()

    func createDoneButton(txtField: UITextField, donePressed: Selector){
let toolbar = UIToolbar() // create toolbar
toolbar.sizeToFit() // toolbar fits the size of the screen

let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: donePressed) // action when the done button was pressed
toolbar.setItems([doneBtn], animated: true)
txtField.inputAccessoryView = toolbar
}

Pass parameter to selector function in Swift

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

}

Sample Image

How to pass parameter to selector in swift?

It is simpler to use block, like

private var _observer: Any!  // unsubscribed automatically on deinit

func addObserver(_ scrollView: UIScrollView) {
_observer = NotificationCenter.default.addObserver(forName: name,
object: nil, queue: nil) { notification in
// ... just use scrollView here
}
}


Related Topics



Leave a reply



Submit