How to make IBAction do nothing until button pressed? - swift

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

Related

Change UIButton color for duration of AVAudioPlayer sound play

I am trying to change the color of a UIButton when pressed. The button triggers AVAudioPlayer and I am trying to get the button color to remain changed until the sound is done playing and then revert back to original color. The code I have changes the button color once pressed and remains at the changed color after the sound is done, but doesn't change back when the player is not playing.
#IBAction func soundPlay(_ sender: UIButton) {
soundPlayer?.play()
if (soundPlayer!.isPlaying == true) {
playButton.backgroundColor = orangeColorLight
}
else {
playButton.backgroundColor = orangeColor
}
}
Per a recommendation in the comments, I've attempted to implement an audioPlayerDidFinishPlaying delegate without success. There is little documentation on how to properly implement this unfortunately so it was pretty much guess work.
#IBAction func soundPlay(_ sender: UIButton) {
playButton.backgroundColor = orangeColorLight
soundPlayer?.play()
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
successfully flag: Bool){
playButton.backgroundColor = orangeColor
}
}
My logic above was to set the lighter color upon button press > play sound > use audioPlayerDidFinishPlaying to watch for sound to finish playing > change color back.
When you say play, you know the sound player is playing, so there is no point following up with a condition; just change the play button color.
Your code is now over. It isn't just going to sit there watching while we wait for the sound to end. There is no such thing as waiting in iOS code. (Well, there sort of is, in extreme cases, but never mind that now.) What you need is to be called back by the sound player when the sound ends.
Well, you can do that! Set yourself as the player's delegate and implement this delegate method:
https://developer.apple.com/documentation/avfoundation/avaudioplayerdelegate/1389160-audioplayerdidfinishplaying
It will be called when the sound ends, and now you can set the color back again in response.
What I have just shown you is the key concept you need to understand in iOS programming. Your code is event-driven. It doesn't stop and wait for things; instead, you need to arrange to hear whenever something happens that interests you. Cocoa provides events to tell you about all sorts of things; your job is put the right code in the right place with the right name so that your code is called when something of interest happens.

Using string for comparison in "if" statement not working (SWIFT)

So I am in the process of developing this app for Mac and I am using Swift. I am just learning + developing the app side by side. I have the main viewController and when you press (+) button, it opens another viewcontroller with textfield. Now i have two buttons, one says "Done" and the other says "Cancel". If you press Cancel the viewController dismisses. However, if you press "Done" then whatever you added in that textfield appears in the first ViewController.
I want to run an if statement on "Done" button. So if you haven't written anything in the textfield and you press done, it gives you an error, and if you have written something, then it allows you to press Done and it dismisses!
I created an IBOutlet for the textfield:
#IBOutlet var weblinklabel: NSTextField!
Then whatever is stored in the weblinklabel is stored in another variable:
weblinklabel.stringValue = done!
Obviously, "done" is an optional string variable, assigned as this:
var done: String? = ""
Now in my understanding, "done" variable if empty should give the error which in this case is a label that shows itself(which is isHidden = false' in viewdidload()) function initially.
This is the "if-statement" that I am trying to use:
if done != nil {
self.view.window?.close()
} else {
label1.isHidden = false
}
I believe the placement of this code isn't right. I am placing it under IBAction of the "Done" button.
Any help in this regard, will be appreciated! thanks
The easiest way to do it would be to use a method similar to what I've explained in another response here.
Basically you would check in controlTextDidChange(notification:) if the content of the text field is "right" and if it's not you disable the Done button. Conversely you enable it if the text field validates, obviously.

Performing two actions with one button at the same time

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.

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.

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.