I have two UI Switches
When I tap one, I want the other to turn off and vice versa
Below I have two switch actions.
I've tried to check if switch one is on (which is by default) and if it is when tapped, then turn it off, but if it isn't on, then turn it on.
#IBAction func switchOnePressed(_ sender: UISwitch) {
if switchOne.isOn {
label.text = "UISwitch is ON"
switchOne.setOn(false, animated: true)
} else {
label.text = "UISwitch is OFF"
switchOne.setOn(true, animated: true)
}
}
#IBAction func switchTwoPressed(_ sender: UISwitch) {}
Any help would be great!
This simplifies things a lot by reducing the code required. The opposite switch is then set based off the current value of the calling switch.
#IBAction func switchOnePressed(_ sender: UISwitch) {
label.text = sender.isOn ? "UISwitch is ON" : "UISwitch is OFF"
switchTwo.setOn(!sender.isOn, animated: true)
}
#IBAction func switchTwoPressed(_ sender: UISwitch) {
label.text = sender.isOn ? "UISwitch is ON" : "UISwitch is OFF"
switchOne.setOn(!sender.isOn, animated: true)
}
You do not need to set the value for the calling switch since that should already be handled by the interface interaction.
Also, I don't know what your intention is with the label, but it would be misleading in its current form since it does not indicate what switch is on & off. Perhaps you have unique labels for each one, but that is just speculation on my part.
Related
There is a taptic which on long press user get vibrate felling with device for a second.
My device is iPhone 6s version 12.6.1 hope it support the taptic.
#IBAction func onClick(_ sender: Any) {
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.prepare()
generator.impactOccurred()
// Do Something
}
The above code is not working!
On Button click I want to use the taptic to make user user tap the button! Same thing I would like to use for picker view and other actions where user validate there inputs.
Let me understand is taptic is same small mechanical movementum?
This is a list of all the possible types of Haptic Feedback you can use. All of these should work. I don't see anything necessarily wrong with your code might be a connection issue as well. Make sure the button is not disabled, its connected and or not behind something.
Also yes the 6s does support Haptic feedback.
#IBAction func errorButtonTapped(_ sender: UIButton) {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
}
#IBAction func successButtonTapped(_ sender: UIButton) {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)
}
#IBAction func warningButtonTapped(_ sender: UIButton) {
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)
}
#IBAction func lightButtonTapped(_ sender: UIButton) {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.prepare()
generator.impactOccurred()
}
#IBAction func mediumButtonTapped(_ sender: UIButton) {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.prepare()
generator.impactOccurred()
}
#IBAction func heavyButtonTapped(_ sender: UIButton) {
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.prepare()
generator.impactOccurred()
}
Make sure this is enabled in settings. If not,
Turn on System Haptic
Go to Settings.
Tap on Sounds & Haptics.
Scroll all the way down to System Haptics.
Toggle this option on.
How to identify in swift if which button was pressed i have 3 button actions.now i have another button is which when pressed it will identify which button from the 3 was pressed. cause first is you have to click from the 3 button after clicking from the 3 button then you will click
idenfifywhichpressed button and this button will identify or print which button from the 3 was pressed.
//this button will identify
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
}
the 3 buttons
#IBAction func btn1(_ sender: UIButton) {
}
#IBAction func btn2(_ sender: UIButton) {
}
#IBAction func btn3(_ sender: UIButton) {
}
Try something like this
Declare an enum
enum SelectedButtonTag: Int {
case First
case Second
case Third
}
Button Action
Connect the three button actions to this method with different tag
#IBAction func idenfifywhichpressed(sender: UIButton) {
switch sender.tag {
case SelectedButtonTag.First.rawValue:
print("do something when first button is tapped")
case SelectedButtonTag.Second.rawValue:
print("do something when second button is tapped")
case SelectedButtonTag.Third.rawValue:
print("do something when third button is tapped")
default:
print("default")
}
}
Small improvement on #prajnaranjan-das answer: casting to an enum immediately to clean up some cruft and remove the need to implement default in the switch...
enum ButtonTag: Int {
case First
case Second
case Third
}
func buttonTapped(_ sender: UIButton) {
guard let knownSender = ButtonTag(rawValue: sender.tag) else { return }
switch knownSender {
case .First:
print("do something when first button is tapped")
case .Second:
print("do something when second button is tapped")
case .Third:
print("do something when third button is tapped")
}
}
This is helped for me (swift 5.0)
#IBAction func hardnessSelected(_ sender: UIButton) {
print(sender.currentTitle) // <----------
}
}
Do something like this,
var index:Int = 0
Now do this, FYI, set button tags accordingly 1,2 and 3...
#IBAction func btn1(_ sender: UIButton) {
index = sender.tag
}
#IBAction func btn2(_ sender: UIButton) {
index = sender.tag
}
#IBAction func btn3(_ sender: UIButton) {
index = sender.tag
}
now this,
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
if index == 1 {
//Button 1 Pressed
}else if index == 2 {
//Button 2 Pressed
} else if index == 3{
//Button 3 Pressed
}
}
FYI. You can use Switch statement as well in idenfifywhichpressed method.
Update. Do not create three methods for btn1, btn2 and btn3, just create one method and assign tag accordingly for simplicity.
I think you can take a variable like
var checkBtn: String?
inside 1st button change the variable value
checkBtn = "1"
inside 2nd button change the variable value
checkBtn = "2"
inside 3rd button change the variable value
checkBtn = "3"
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
// just print the value of CheckBtn
print(checkBtn)
//this will give you which btn pressed right
}
In my code, when a view disappears, a specific action occurs. I am doing it through the viewDidDisappear() function.
I have a specific button that when is pressed it goes to another view. I was wondering in what I way I could tell ONLY the function caused by a specific button to skip the viewDidDisappear().
I perfectly know I can add a sort of 'if' statement in the viewDidDisappear() but I was wondering if there was a more efficient method.
viewDidDisappear() is a UIViewController's lifecycle callback method that's called by the environment - as far as I know there is no way to disable its calling. And I don't think there should be - as I mentioned, it is a part of UIViewController's lifecycle, not calling it would break the contract - see its documentation.
Therefore you have to (and you should) achieve what you want by using if statement.
Do something like this:
fileprivate var skipDisappearingAnimation = false
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
prepareInterfaceForDisappearing()
}
fileprivate func prepareInterfaceForDisappearing() {
guard !skipDisappearingAnimation else {
// reset each time
skipDisappearingAnimation = false
return
}
// do the stuff you normally need
}
#objc fileprivate func buttonPressed(_ sender: UIButton) {
skipDisappearingAnimation = true
// navigate forward
}
It cannot be done; you must handle the case manually with if, something like:
var shouldSkip: Bool = false
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if !shouldSkip {
// your code goes here
}
shouldSkip = false // don't forget to set should skip to false again
}
#IBAction func buttonDidTap(_ sender: Any) {
shouldSkip = true // this will avoid run your code
// your code here
}
I'm creating a music app and I'm using an UISlider for the music volume control. the problem is when I press the volume button on the iPhone / iPad UISlider does not change or move. I've been looking for where to get no referrals.
This is the code I use:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.slider.setValue(AVAudioSession.sharedInstance().outputVolume, animated: true)
}
}
#IBAction func volumeControlSlider(_ sender: UISlider) {
slider = (MPVolumeView().subviews.filter { NSStringFromClass($0.classForCoder) == "MPVolumeSlider" }.first as! UISlider)
slider.setValue(sender.value, animated: false)
}
I have taken a UISwitch control in my new swift project. And the default state of this is set as Off from storyboard. I have bounded it as IBOutlet
#IBOutlet weak var SwitchRemFacility: UISwitch!
I have also bounded it to a method by Value change event from storyboard.
#IBAction func rememberFacilityClick(sender: AnyObject) {
print(SwitchRemFacility.on)
if SwitchRemFacility.on {
SwitchRemFacility.setOn(false, animated: true)
} else {
SwitchRemFacility.setOn(true, animated: true)
}
}
The Problem is that SwitchRemFacility.on returns always true here. I have checked by breakpoint and also I have printed it to console. Any hep will be appreciated.
Thanks in advance.
You are testing it when value is already changed. Switch will do it's on and off work by it self.
If you want to do it manually you can do like :
#IBAction func switchChangeVal(sender: AnyObject) {
let swh : UISwitch = sender as! UISwitch
if(swh.on){
swh.setOn(true, animated: true)//But it will already do it.
}
else{
swh.setOn(false, animated: true)
}
}
Actually no need of code. But if you want to condition on the on or off state then you have to do code.
For Example
#IBAction func switchChangeVal(sender: AnyObject) {
let swh : UISwitch = sender as! UISwitch
if(swh.on){
//Do the code for when switch is on
}
else{
//Do the code for when switch is off
}
}