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 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
}
Pass arguments in @selector method
You can take help of reference variable for your textfield. Declare a global property as textfield. Keep a refernce of currently active textfield and access it in every method as per your requirement.
UITextField *activeTextField;
// UITextField Delegates
-(void)textFieldDidBeginEditing:(UITextField *)textField {
activeTextField = textField;
}
You can use this activeTextField property in your class anywhere.
You need to define your class as delegate of UITextField.
self.yourTextField.delegate = self
Passing arguments to selector in Objective-C
The short answer is that you don't. methods like performSelector: are set up to pass NSObjects as parameters, not scalar values.
There is a method performSelector:withObject:withObject: that will invoke a method with 2 object parameters.
You can trigger a method with any type of parameters by creating an NSInvocation, but it's a fair amount of work, and a little tricky to figure out how to do it.
In your case it would be simpler to refactor your method to take the ID parameter as an NSNumber object.
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
}
Related Topics
API Call Error in Xcode 7/iOS 9 (How to Setup App Transport Security in Plist)
Testflight Sdk and iOS Simulator - How to Use
Raw Depth Map Sdk for iPhone X
-Webkit-Overflow-Scrolling: Touch' Broken for Initially Offscreen Elements in iOS7
How to Figure Out When a HTML5 Video Player Enters The Full Screen Mode on iOS/Ipads
Uiviewrepresentable Automatic Size - Passing UIkit UIview Size to Swiftui
How to Open Application Using Url
Using String.Characterview.Index.Successor() in for Statements
Set Custom Font for UItableview Swipe Action (Uicontextualaction)
How to Create a Scnnode from a .Usdz
Swift 3: Transfer Utility Enumeratetoassignblocks Method Signature
Xcode UI Test Uikeyinput Typetext
Uitabbar Change Background Color of One UItabbaritem on iOS7