Performing two actions with one button at the same time - swift

Im creating an application where I want to perform two actions at the same time with one button like it is said in the title but I don't have any idea to do that.
Here is a part of my code:
//Function Test button: animate the button while playing the song
#IBAction func testButton(sender: AnyObject) {
self.ButtonAudioPlayer.play()
animView.startCanvasAnimation()
}
Thank you for your answers !

You can call the other action from within the first action. For example, I want a button to perform its own action plus the action of another button:
#IBAction func buttonOneAction(sender:AnyObject){
self.doButtonOneThing()
self.buttonTwoAction(sender)
}
#IBAction func buttonTwoAction(sender:AnyObject){
self.doButtonTwoThing()
}
When button one is pressed it will do something then it calls button two's action, preforming both. #IBActions are just functions like any other. #IBAction is strictly used by interface builder.

Related

How to make IBAction do nothing until button pressed?

I am making a swift program that rolls die when a button is tapped but before that it asks them if they believe they will roll snake eyes, yes or no.
I want to know if its possible that when yes or no is tapped it will wait for the user to tap the roll button and then execute a response based on that? I am confused on how to add an IBAction pressed.
I have tried to add the yes button IBAction into the roll button IBAction and have ended up with many errors.
#IBAction func yesButton(_ sender: Any) {
if(diceImageView1.image == diceImageView2.image){
resultText()
} else {
lossText()
//functions changes label to message telling user if they rolled snake eyes or not
}
}
I expect the user to be able to tap a response and then have to tap the roll button for the label to change.
Present a UIAlertViewController. Then do logic depending on the button.
See guide
https://medium.com/ios-os-x-development/how-to-use-uialertcontroller-in-swift-70143d7fbede

xcode / swift receiving user data through a form

I've been designing an app where the user needs to be able to send me the value of certain variables within the app. I originally planned to use a simple mail setup where they fill out a form and the game data is automatically filled out in the email.
Is there any way a user can click a button, and send me the values of variables within the app without the user specifically seeing or being able to modify the variables?
The user consents and realizes what they are doing to. I've been looking at firebase and seeing if there's anything there which can help me. Any ideas would be greatly appreciated.
Is there any way a user can click a button, and send me the values of
variables within the app without the user specifically seeing or being
able to modify the variables?
Yes. Add a UIButton to your storyboard and create a IBAction for it. Whenever the button gets tapped the IBAction gets called. In this case the IBAction is named didTapbutton. See the code below:
import UIKit
class ViewController: UIViewController {
var secretName = "Bond"
var secretNumber = 007
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func didTapButton(_ sender: Any) {
mail(to: secretName, secretNumber: secretNumber)
}
func mail(to secretName: String, secretNumber: Int) {
print("hello \(secretName + secretNumber)")
}
}
didTapButton uses the local variables secretName and secretNumber "within the app" without the user seeing the values.

Xcode 7.2.1 Making a Reset Button using a Button with Swift

I am trying to make a reset button for my app that will reset the UI to the original state. I have made a UIButton and linked it to the ViewController, but I have no idea where to go from here. I tried using the following code:
#IBAction func resetToOriginalState(sender: UIButton) {
self.resetToOriginalState (sender: UIButton)
}
It gave me the following error:
Editor placeholder in source file
Sorry if there may be an obvious answer, but I am very new to Swift and Xcode.
Is there any other way to create a reset button?
The error:
Editor placeholder in source file
Is because you are calling a function with the UIButton Class name instead of the actual button.
#IBAction func resetToOriginalState(sender: UIButton {
// this line is wrong, you shouldn't have UIButton in here
self.resetToOriginalState (sender: UIButton)
// the line should read
self.resetToOriginalState (sender: sender)
}
This way, you are passing the actual button into the function that was passed to resetToOriginalState
Seems you have too IBAction for the same button, check how many time you have #IBAction func resetToOriginalState(sender: UIButton) in your code and remove the references from the references Interface list to clean it, should there be only one :
It depends what is in the scene and what do you need to reload. As far is I know you can't really segue a ViewController to itself, but here are few options:
Try to add loadView() when the button is pressed
Duplicate the view controller, and segue between the two. (might be risky and create more work)
Reset your variables to their initial state when the button is pressed
You should give us more detail because this is implementation specific.
Nevertheless, it's not very clean, but depending on the architecture of your code, you might be able to generate a new instance of your view controller, destroy the current one, and present the new one.

How to trigger a binded action from a button?

I have a subclass of NSButton, but I don't want to binded action to be triggered when user will make mouse down/up on the button.
How can I trigger that from another function?
Just change the function declaration to something like this:
#IBAction doSomething() {
// ...
}
You can both connect it to your NSButton instance, as well as call it from your code with doSomething().

Simulate pressing a button in swift

I have the following method
#IBAction func clearDisplay(sender: UIButton)
and I want to overload this method so that I can clear the display if some conditions are satisfied in my code. So
func clearDisplay()
My idea is to simply simulate pressing the clearDisplay button but am not sure how to do that. Does this sound like a good idea? How would I simulate pressing the button?
As Matt says, just call the IBAction method directly. You should probably change the object type of sender to AnyObject, and then use
clearDisplay(self)
Which will pass in the view controller as the sender.
In your clearDisplay method, if you have code that does something with the button, check it's type first to make sure it really is a button.