How to hide an action button in Swift? - swift

I try to make a button invisible after pressing it. The Connection is an action and not an outlet, because pressing the button will call additional code.
#IBAction func startGame(_ sender: Any) {
print("The game starts...")
}
This does not work, because the button is an action and not an outlet:
startGame.isHidden = true
Is there another way to make an action button invisible and therefore not clickable?

Just create an IBOutlet of the same button and set its isHidden property to true once it's tapped.
#IBAction func startGame(_ sender: Any) {
startGameButton.isHidden = true
}

You can hide button on pressed action this way
#IBAction func startGame(_ sender: Any) {
let pressedButton : UIButton = sender as! UIButton
pressedButton.isHidden = true;
}

You can rewrite your code a little bit as Pratik suggested, so it will look like this:
#IBAction func startGame(_ sender: UIButton) {
sender.isHidden = true
/*
remove button at all from the parent view.
sender.removeFromSuperview()
*/
print("The game starts...")
}

Related

How to disable a button within a anyObject Button in swift

Ok so I want to be able to disable the button within itself. So when I press the button it will disable itself.
import UIKit
class ViewController: UIViewController {
#IBOutlet var buttons: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func but(_ sender: AnyObject) {
//this code doesn't seem to work at all, and I don't know why
(sender as AnyObject).isEnabled = false
}
}
It gives me an error message when ever I try to do this "Cannot assign to immutable expression of type '#lvalue Bool?'"
So is there a different way I can disable this button
Presumably, you've hooked up this IBAction in Interface Builder to a UIButton. So, you can change your function signature to:
#IBAction func but(_ sender: UIButton) {
sender.isEnabled = false
}
AnyObject doesn't have a property called isEnabled, which is why your previous code failed.
within the button I use this code
#IBAction func but(_ sender: AnyObject) {
var disableMyButton = sender as? UIButton
disableMyButton?.isEnabled = false
}

How to update a Stepper value from another function in Swift

How do I update the "intensityStepper" stepper value to 0 from the "firstSignal" button function?
#IBAction func intensityStepper(_ sender: UIStepper) {
//code
}
#IBAction func firstSignal(_ sender: Any) {
//code
}
Connect the outlet to your controller and not only the IBAction (select outlet and not action from the dropdown after you control drag). This way you can access the stepped from the other function.

How can I use a segue when I have two buttons that need to be pressed to activate it?

Basically, what I want to do is activate a certain segue when the user presses 2 different buttons. Unfortunately, I only know how to do it when 1 button is pressed. How can I achieve this? Thanks in advance!
I'm assuming you're supposed to press these buttons sequentially.
Create 2 boolean variables in your ViewController and 2 IBAction methods, one for each button.
When button A or B is pressed, set the corresponding boolean to true.
When the second button (either A or B) is pressed, check if the other button's boolean variable is true. If yes, trigger the segue programmatically.
You can create this segue between the two view controllers in the storyboard. Remember to give the segue a fitting name.
Here's some example code.
class MyViewController: UIViewController {
var buttonAIsTapped: Bool = false
var buttonBIsTapped: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func buttonATapped(_ sender: UIButton) {
buttonAIsTapped = true
if buttonBIsTapped == true {
self.performSegue(withIdentifier: "SegueName", sender: self)
}
#IBAction func buttonBTapped(_ sender: UIButton) {
buttonBIsTapped = true
if buttonAIsTapped == true {
self.performSegue(withIdentifier: "SegueName", sender: self)
}
}

Get title of a navigation bar button item in swift?

I am not able to get navigation bar button item, when I created action outlet.Here is my code
#IBAction func rightBarButtonItemDidTap(_ sender: Any) {
print(sender.title)
}
but title is not available.
Try this
#IBAction func rightBarButtonItemDidTap(_ sender: Any) {
let barButton = sender as? UIBarButtonItem
if let title = barButton?.title {
print(title)
}
}

swift becomeFirstResponder

I need to open the keyboard.
When the ViewController starts I use this function
func showKeyboardAndFocus(){
DispatchQueue.main.async {
self.placeForText.resignFirstResponder()
self.placeForText.becomeFirstResponder()
}
}
from viewDidLoad and it works correctly.
When I close it, and try to open using the same function, it doesn't work.
Why? How can I open it second time?
To show the keyboard:
self.inputTextView.becomeFirstResponder()
To hide the keyboard:
self.inputTextView.resignFirstResponder()
Show/hide second time
#IBAction func showKeyboardClicked(_ sender: UIButton) {
textField.becomeFirstResponder()
}
#IBAction func closeKeyboardClicked(_ sender: Any) {
textField.resignFirstResponder()
}