Passthrough _ sender UIViewController action method - swift

How can I pass through the sender of an action outlet like this:
#IBAction func doPlay(_ sender: Any) {
someMethod(???)
}
What means the underscore?

just write
someMethod(sender)
The underscore just means that the argument "sender" doesn't need a label.

Related

How to Pass the data given in UITextField to another variable on another class

So i have a UITextField named apiTextField and a function of the saveButton :
func saveButton(_ sender: Any) {
}
I want when the user writes to the UITextField and press the saveButton that text the user wrote be passed to a variable which is called var baseURL:String? in another class.
I didn't find anything related to UITextField so i decided to make this question, another similar one is 10 years old!.
var anotherClass = AnotherClass()
func saveButton(_ sender: Any) {
guard let text = apiTextField.text, !text.isEmpty else { return }
anotherClass.baseURL = text
}
Is that what you are looking for?

Access a variable made in another IBAction in the same file

I've created a variable in one IBAction which saves a string, and I want to be able to call that variable in another IBAction in the same file. How do I define the variable globally so that the other IBAction can call it?
The variable message comes from a UIAlertController output, which appears when pressing a button on the View Controller.
#IBAction func EditMessage(_ sender: Any) {
let message = Message(message: text)
}
#IBAction func PostArticle(_ sender: Any) {
let parameters = ["title": "subheading", "content": "\(message)"]
}
Trying to call the message variable from the other IBAction will only give the error:
Use of unresolved identifier 'message'
class MyViewController : UIViewController {
var message = ""
#IBAction func EditMessage(_ sender: Any) {
message = Message(message: text)
}
#IBAction func PostArticle(_ sender: Any) {
let parameters = ["title": "subheading", "content": "\(message)"]
}
}
Hope it Helps.
Make your message variable be accessible to the two IBActions.
Your Message function should return a string
Message(message: String) -> String {
// Do something here
return "sample"
}
Read Swift Function

Different definition for sender

So I used sender a lot in swift but it confuses what exactly it does.
#IBAction func btnPressed(_sender: AnyObject){
performSegue(withIdentifier: "newScreen", sender: code)
}
Someone explain the difference between the two senders pls. It kind of confuses me because it has the same name but different functions.
The parameter of an IBAction tells you who caused the action. In your case, when you push the button that triggered this IBAction to be invoked, sender will be set to that button.
The word sender is nothing more than a name. It's a typical convention.
In your case, it's better to use a more strongly typed argument, with a more descriptive name, such as:
#IBAction func btnPressed(_ button: NSButton) { // or UIButton for iOS
performSegue(withIdentifier: "newScreen", sender: code)
}

modify attributes of sender in swift

How can attributes be modified from the sender in swift? For example, if I have a multiple buttons that are connected to the same event handler, how can I modify the attributes of the button (say, the title) that was pressed?
#IBOutlet weak var grade_preK: UIButton!
#IBAction func gradeButtonPressed(sender: AnyObject) {
sender.title = "New Title"
}
The handler here returns the error "Cannot assign to 'title' in 'sender'". How then, can attributes be changed on the sender of the event?
When you created this, Interface Builder may have given you the option to declare sender to be UIButton rather than AnyObject (it does have that option; you may not have noticed it). You could have chosen that, or you can fix it now:
#IBAction func gradeButtonPressed(sender: UIButton) {
And now sender is of the right type so you can modify it (and it is reasonable style to do so in Cocoa).
(Note that UIButton actually has a setTitle(_ title: String?, forState state: UIControlState) method, not a setTitle() method, so that's what you probably meant to call.)

Swift - call #IBAction method in viewDidLoad without parameter

#IBAction func getNewPhotoAction(sender: AnyObject) {
println("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(sender: AnyObject) // Error
}
I just want to call the getNewPhotoAction IBAction method in viewDidLoad.
Which parameter to enter in this line -> self.getNewPhotoAction(?????) ?
I don't have any parameter. I just need to call.
I used in Objective-C style:
[self getNewPhotoAction:nil]
but I don't know Swift style.
The parameter sender indicates who are calling the action method.
When calling from viewDidLoad, just pass self to it.
override func viewDidLoad() {
super.viewDidLoad()
getNewPhotoAction(self)
}
By the way, if the sender parameter of the getNewPhotoAction method wasn’t used, the parameter name can be omitted.
#IBAction func getNewPhotoAction(AnyObject) {
println("getNewPhotoAction")
}
You could always create a separate func that you call on in your viewDidLoad or in your IBAction
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhoto()
}
func getNewPhoto(){
//do whatever you want here.
println("getnewphotoaction")
println("whatever you want")
}
#IBAction func getNewPhotoAction(sender: AnyObject) {
self.getNewPhoto()
}
Swift 4.2
#IBAction func getNewPhotoAction(sender: Any) {
println("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(AnyObject.self)
}
If you still need to reference the UIButton or whatever is sending the action and want to call it from code at the same time - you can do this too:
onNext(UIButton())
Wasteful, but less code.
#IBAction func getNewPhotoAction(sender: AnyObject?){
......
}
**AnyObject** means that you have to pass kind of Object which you are using, nil is not a AnyObject.
But **AnyObject?**, that is to say AnyObject is Optional, nil is a valid value.
meaning the absence of a object.
self .getNewPhotoAction(nil)
You actually don't need to pass any object at all. If you have no need to use the sender, then declare the function without it like this:
#IBAction func getNewPhotoAction() { ... }
And use it like this:
self.getNewPhotoAction()
You may need to re-connect the outlet in interface builder when you make this change (remove it and then add it back) if this method is connected to an event in interface builder.
#IBAction func getNewPhotoAction(sender: AnyObject? = nil) {
print("getNewPhotoAction")
}
override func viewDidLoad() {
super.viewDidLoad()
self.getNewPhotoAction(nil)
}
Since you don't have any sender, hand over nil.
self.getNewPhotoAction(nil)