swift becomeFirstResponder - swift

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()
}

Related

Why doesn't performSegue() call shouldPerformSegue()

Quite new to programming in Swift and mobile development in general. I am trying to use performSegue() and control it without if-else statements. I made a google search how to use override func shouldPerformSegue() and tried to implement it in different ways but none of them solved my situation. Here I need to segue to Yellow or Green views if the switch is on using corresponding buttons. Even if I made return false, the function does not cancel the segue to happen. What is the reason of this behaviour and how can I fix it? Many thanks.
class ViewController: UIViewController {
#IBOutlet var segueSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func yellowButtonTapped(_ button: UIButton) {
performSegue(withIdentifier: "Yellow", sender: nil)
}
#IBAction func greenButtonTapped(_ button: UIButton) {
performSegue(withIdentifier: "Green", sender: nil)
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
// return segueSwitch.isOn
return false
}
}
shouldPerformSegue does not get called if you use performSegue(withIdentifier: in code. This function only get called if the segue is triggered from storyboard.
You can easily test this by adding 2 Buttons to a ViewController in storyboard one with the storyboard segue and one with an IBOutlet to a new ViewController. Then add a print statement to shouldPerformSegue. In the Button outlet call performSegue. Only the first button performing the segue from storyboard will print to the console.
And additionally you don´t need it. Any validation you would perform in shouldPerformSegue can be done in the ...ButtonTapped function:
#IBAction func yellowButtonTapped(_ button: UIButton) {
if validateSegue(){
performSegue(withIdentifier: "Yellow", sender: nil)
}
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
validateSegue()
}
func validateSegue() -> Bool{
......
}
This is deliberate and makes perfect sense. shouldPerformSegue lets you prevent an automatically triggered segue in case you don't want it performed. But if you didn't want a manual segue performed, all you had to do is nothing — don't say performSegue in the first place.
and control it without if-else statements
If-else is exactly how to control it.

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)
}
}

How to hide an action button in 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...")
}