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

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

Related

Show password typed by user in UITextfield in Swift

I have password UITextfield that is currently turned on as secure entry. I would like to show the user the password he has typed inform of text again in the UITextfield when the UISwitch is turned on. Here is my implementation that so far. It works when i print it out in the console but doesn't in the UITextfield. I would like to show it on once the UISwitch is turned on and off when the UISwitch is turned off.
#IBOutlet weak var existingPasswordTexfField: UITextField!
#IBOutlet weak var newPasswordTextField: UITextField!
#IBOutlet weak var changePasswordSwitch: UISwitch!
#IBAction func showPassword(_ sender: UISwitch) {
if changePasswordSwitch.isOn {
guard let oldText = existingPasswordTexfField.text else { return }
if existingPasswordTexfField.isSecureTextEntry {
existingPasswordTexfField.text = oldText
} else {
print("Pawword is already secure")
}
}
}
According to Apple Doc ìsSecureTextEntry is a writable property
So you in your showPassword IBAction you need to set it toggle it at some point :
existingPasswordTexfField.isSecureTextEntry = false
or just
existingPasswordTexfField.isSecureTextEntry = !changePasswordSwitch.isOn
You'll need to change the isSecureTextEntry flag on the textfield when the user flips the switch.
#IBAction func showPassword(_ sender: UISwitch) {
existingPasswordTextField.isSecureTextEntry = changePasswordSwitch.isOn
}
This is also works
existingPasswordTexfField.isSecureTextEntry = changePasswordSwitch.isOn ? false : true

Disable all UIButtons in UITableViewCell once a separate UIButton is pressed

Problem I've found some questions asking how to disable a particular cell button in a table view, but what I want to do is to disable all instances of a button within a table view cell when another button is pressed.
Details I have a table view which is displaying a list of exercises and number of reps in a custom cell. Within the custom cell is also a button "swap" which allows a user to swap an exercise for another one before the workout starts.
When the user hits "start workout" (which triggers a timer) I want to disable all instances of the swap button (grey them all out and make non clickable).
Code
My workout cell class is here :
class WorkoutCell : UITableViewCell {
var delegate: WorkoutCellDelegate?
#IBAction func swapButtonPressed(_ sender: Any) {
delegate?.swapButtonTapped(cell: self)
}
#IBOutlet weak var exerciseName: UILabel!
#IBOutlet weak var repsNumber: UILabel!
}
protocol WorkoutCellDelegate {
func swapButtonTapped(cell: WorkoutCell)
}
What have I tried
The way I thought to do this was to add an IBOutlet (e.g. 'swapButton') for the button and then simply do something like :
#IBAction func startButtonPressed(_ sender: UIButton) {
WorkoutCell.swapButton.isenabled = false
}
But Xcode doesn't allow you to add IBOutlets to repeating cells so I'm a bit stuck.
I'm fairly new to delegates (managed to get it working for displaying the table view) so if it has something simple to do with that sorry!
Add a property to your viewcontroller:
var swapButtonsDisabled : Bool = false
In your cellForRow do something like this:
cell.swapButton.isEnabled = !self.swapButtonsDisabled
When the start button is pressed, set swapButtonDisabled to true and reload the tableView.
1- As you connect
#IBOutlet weak var exerciseName: UILabel!
create outlet for every btn
#IBOutlet weak var btn1: UIButton!
2- Add a property to the model array in the VC to hold the state of every cell
3- When you click the main btn fire the delegate method with the btn's cell
4- In VC delegate handle method disable the btns and change the state of the array index path
5- Don't forget to check state in cellForRow
You are pretty close. First I suggest you to be more specific and have the data you need in cell and use access control:
class WorkoutCell : UITableViewCell {
var workoutSwappable: (workout: Workout, canBeSwapped: Bool)? {
didSet {
swapButton.isEnabled = workoutSwappable?.canBeSwapped == true
// TODO: do additional setup here
}
}
weak var delegate: WorkoutCellDelegate? // Needs to be weak or you will have memory leaks
#IBAction private func swapButtonPressed(_ sender: Any) {
if let workoutSwappable = workoutSwappable, workoutSwappable.canBeSwapped == true {
delegate?.workoutCell(self, didTapWorkoutSwap: workoutSwappable.workout)
}
}
#IBOutlet private var exerciseName: UILabel!
#IBOutlet private var repsNumber: UILabel!
#IBOutlet private var swapButton: UIButton!
}
Ok so now in cell for row at index path all you need is something like:
cell.workoutSwappable = (self.items[0], self.canSwap)
On delegate you now have:
func workoutCell(_ sender: WorkoutCell, didTapWorkoutSwap workout: workout) {
self.currentWorkout = workout
self.canSwap = false
self.initializeTimer()
tableView.reloadData() // This will now flush all the buttons
}

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

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.

Referencing a button in Swift

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

Resources