Referencing a button in Swift - ios

So I'm pretty new but I'm trying to reference a button from another. I disable the first button when I press it, but I want to enable it when I press the other button. Check this out. It doesn't like my second sender reference.
#IBAction func IBbtnUpdateTap(sender: UIButton){
sender.enabled = false
}
#IBAction func addSpinsButton() {
sender.enabled = true
}
Any ideas from you coding wizards?

The problem is that the context of your function addSpinsButton as no sender variable. You must add a reference to your button in the class.
You can do this with something like:
#IBOutlet weak var myButton: UIButton!
Link the button in interface builder (same way you have done with your function, but for the outlet)
Then your addSpinsButton will become:
#IBAction func addSpinsButton() {
myButton.enabled = true
}

what you need is something like this:
class ViewController: UIViewController {
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBAction func button1Tapped(sender: UIButton) {
sender.enabled = false
}
#IBAction func button2Tapped(sender: UIButton) {
button1.enabled = true
}
}

Related

How to unhide the button after all switches in ON condition?

I have 2 UISwitch and a Button, in viewDidLoad, I set the button to be hidden and disabled, I want only my button to be not hidden if those 2 switch is in ON state, otherwise, I want my button to hide again. is there any method from UI Switch delegate that can be used ? how do I do that in Swift ?
here is the code I use
import UIKit
class AskingAuthorizationVC: UIViewController {
#IBOutlet weak var locationSwitch: DesignableSwitch!
#IBOutlet weak var notificationSwitch: DesignableSwitch!
#IBOutlet weak var nextButton: DesignableButton!
override func viewDidLoad() {
super.viewDidLoad()
// initial state
nextButton.isHidden = true
nextButton.isEnabled = false
notificationSwitch.isOn = false
locationSwitch.isOn = false
}
#IBAction func signUpButtonDidPressed(_ sender: Any) {
performSegue(withIdentifier: "toAuthenticationVC", sender: nil)
}
}
Hook both of the UISwitch-s as IBActions & IBOutlets
#IBAction func oweSwitch(_ sender: UISwitch) {
self.mybutton.isHidden = !(switch1.isOn && switch2.isOn)
}

Show and hide UIButton

I use this code so that when I click on button 1, button 2 is hidden
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button1: UIButton!
#IBAction func button1(_ sender: Any) {
button2.isHidden = false
}
How can I make it so that when you press button 1 again, button 2 is displayed.
You can simply use this tricks:
#IBAction func button1(_ sender: UIButton) {
button2.isHidden = !button2.isHidden
}

Action triggered when two buttons are tapped

How can I trigger an action in Swift when two buttons are pressed simultaneously? My code below triggers the action when one button is pressed. I would like this action to trigger only if both buttons are tapped.
class ViewController: UIViewController {
#IBOutlet weak var leftButton: UIButton!
#IBOutlet weak var rightButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
leftButton.addTarget(self, action: #selector(ViewController.leftButtonClicked), forControlEvents: .TouchUpInside)
}
func leftButtonClicked() {
// Do something
}
}
Do I need to use UIGestureRecognizer or can this be done with the addTarget action above? How would the code look like?
With the help of the comment above I have now written the solution in Swift and it turns out to be very simple:
class ViewController: UIViewController {
#IBOutlet weak var leftButton: UIButton!
#IBOutlet weak var rightButton: UIButton!
#IBAction func touchButton(sender: AnyObject) {
if leftButton.touchInside && rightButton.touchInside {
print("Two buttons pressed")
}
I removed the clutter that is not relevant for the solution.

Swift: Checking UISwitch on mode by regarding other UISwitch's status

I have more than one UISwitch on the same view. On my form, I need to use UISwitch like radio button (web). No more than one UISwitch can be on at he same time, I need just one.
If a UISwitch turning on, I want to check others and if there are any UISwitch with on mode, need to turn it off. Is it possible?
I can check it in a button action but, I want to change UISwitch status real time.
UPDATE AFTER SOLVED:
Here is my code:
#IBOutlet weak var firstSwitch: UISwitch!
#IBOutlet weak var secondSwitch: UISwitch!
#IBAction func firstSwitchAction(sender: AnyObject) {
if self.secondSwitch.on == true {
self.secondSwitch.setOn(false, animated: true)
}
}
#IBAction func secondSwitchAction(sender: AnyObject) {
if self.firstSwitch.on == true {
self.firstSwitch.setOn(false, animated: true)
}
}
You need to create outlets for your switches, you can change them with the .setOn method
A quick example
#IBOutlet weak var mySwitch: UISwitch!
#IBOutlet weak var myOtherSwitch: UISwitch!
#IBAction func mySwitchTouched(sender: UISwitch) {
if sender.on == true {
myOtherSwitch.setOn(false, animated: true)
}
}

Swift: Value Changing Control Events Not Calling?

So I have added targets to my IBActions I have created that occur when the value of a text field changes. When these actions occur, the system should check if the two text fields are both integers. I have set two variables set to false, and they are set to true when both of them are an int. In the IBActions, I have if statements that tell a button to be enabled if both of the variables contain integers. When I run the simulator, this button doesn't enable when both of the text fields contain an integer.
I am new to swift, so if possible, please write all of the code out and where it should be in my code. Here is what I have so far:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var calculatorButton: UIButton!
#IBOutlet weak var inspirationLabel: UILabel!
#IBOutlet weak var beginningLabel: UILabel!
#IBOutlet weak var calculatorContainer: UIView!
#IBOutlet weak var answer1Label: UILabel!
#IBOutlet weak var doneButton: UIButton!
#IBOutlet weak var yourWeightTextField: UITextField!
#IBOutlet weak var calorieNumberTextField: UITextField!
#IBOutlet weak var menuExampleButton: UIButton!
#IBOutlet weak var aboutButton: UIButton!
#IBOutlet weak var calculateButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
yourWeightTextField.delegate = self
calorieNumberTextField.delegate = self
calculateButton.enabled = false
// Calling the textfield valueChanged Methods
yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.ValueChanged);
calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.ValueChanged);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func calculatorButtonTapped(sender: AnyObject) {
calculatorContainer.hidden = false
inspirationLabel.hidden = true
beginningLabel.hidden = true
menuExampleButton.hidden = true
aboutButton.hidden = true
}
var yourWeightFilled = false
var calorieNumberFilled = false
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// Find out what the text field will be after adding the current edit
let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
// If the textfields have the properties of the function
if textField == yourWeightTextField {
yourWeightFilled = text.toInt() != nil
} else if textField == calorieNumberTextField {
calorieNumberFilled = text.toInt() != nil
}
return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField.resignFirstResponder();
return true;
}
// The methods to close the keyboard when editing is finished
#IBAction func yourWeightEditingDidEnd(sender: AnyObject) {
yourWeightTextField.resignFirstResponder()
}
#IBAction func calorieNumberEditingDidEnd(sender: AnyObject) {
calorieNumberTextField.resignFirstResponder()
}
#IBAction func yourWeightValueChanged(sender: AnyObject) {
// If both variables are true and the text fields contain integers, enable button
if self.yourWeightFilled && self.calorieNumberFilled {
self.calculateButton.enabled = true
}
}
#IBAction func calorieNumberValueChanged(sender: AnyObject) {
// If both variables are true and the text fields contain integers, enable button
if self.yourWeightFilled && self.calorieNumberFilled {
self.calculateButton.enabled = true
}
}
}
You should look for EditingChaged event, not ValueChanged
EDIT:
What I mean is to change from:
yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.ValueChanged);
calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.ValueChanged);
to :
yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.EditingChanged);
calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.EditingChanged);
You simply are looking for wrong event.
If you are looking for a text changed event , then Right Click on the text field select Editing Did End from the Sent Events . You can see a circle on the right end click the circle Hold Down Ctrl and Drag it to your ViewController file. Name the Action you want and . I have provided some screen shots for this.
Here i name the Action TextChanged
I am Using Xcode 7 Swift 2 here
Right Click on the Text Box and You can see Something Like this
Finally You can see the TextChanged event Created. when you type something on a text box and click return this event fires.

Resources