I am trying to use a UIButton as a switch. When the button is tapped, the button image should change to (atcs.png) and state should be (.selected).
When image is tapped again, the button image should again to (actns.png) and state should be (.normal).
Below is an example of my current code:
#IBOutlet weak var atcBtn: UIButton!
#IBAction func atcTapped(_ sender: UIButton) {
if atcBtn.isSelected {
atcBtn.setImage(UIImage(named: "atcs.png"), for:.selected)
} else {
atcBtn.setImage(UIImage(named: "atcns.png"), for:.normal)
}
}
This somehow isn't working. Is there anything missing? Tried almost every solution listed on here, but nothing.
First, move your setImage code to viewDidLoad. There's no reason to repeatedly set these images:
atcBtn.setImage(UIImage(named: "atcs.png"), for:.selected)
atcBtn.setImage(UIImage(named: "atcns.png"), for:.normal)
Next, toggle the isSelected property when the button is tapped:
#IBAction func atcTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
}
Also of note - use sender instead of hardcoding atcBtn.
Related
I'm new to swift programming and I'm trying to disable the save button until all other required buttons are selected, but when I try to do that with the button.isEnabled = false, it doesn't change even when all the buttons are selected. here is a sample code:
func disableButton () {
if firstButton.isSelected && rightButton.isSelected {
saveButton.isEnabled = true
} else {
saveButton.isEnabled = false
}
}
when I remove the last line the save button works but when I put it back it's disabled even when the two other buttons are selected.
Assuming you have used Storyboard you have to link the 2 buttons into #IBActions , and within those #IBAction methods manipulate the isSelected property. Refer the example below.
NOTE - Read the comments
class ViewController: UIViewController {
// 2 buttons that we will set the isSelected property
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
// Button to disable/enable
#IBOutlet weak var finalButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
finalButton.isEnabled = false // setting the button as disabled
}
/// This is the function triggered when you click on the "button1"
#IBAction func didPressButton1(_ sender: UIButton) {
// Here we will set the isSelected property of "sender" parameter, which is the button that calls this function. That is button 1
sender.isSelected = sender.isSelected ? false : true
//calling this function to make any updates to the UI if needed
disableButton()
}
/// This is the function triggered when you click on the "button2"
#IBAction func didPressButton2(_ sender: UIButton) {
// Here we will set the isSelected property of "sender" parameter, which is the button that calls this function. That is button2
sender.isSelected = sender.isSelected ? false : true
//calling this function to make any updates to the UI if needed
disableButton()
}
func disableButton () {
if button1.isSelected && button2.isSelected {
finalButton.isEnabled = true
} else {
finalButton.isEnabled = false
}
}
}
What happens here is you will set the isSelected property of the button that calls the function in the function itself, and run the disableButton() function every time it is invoked for UI updates.
The final result will be,
I have 16 button. And I want to change background color of button, when button is clicked.
This is my code:
#IBOutlet var buttons: [UIButton]!
#IBAction func button_Taped(_ sender: UIButton) {
}
}
How can I change when clicked the buutton change background color of button
#IBOutlet var buttons: [UIButton]!
#IBAction func button_Taped(_ sender: UIButton) {
sender.backgroundColor = // your desired color
}
}
I have searched the web and youtube about segmented control, but I only find examples of how to change to a different view.
My goal is to take the selected value from the segmented control and use it in the action on another button.
I will try to explain what I mean :
#IBAction func segmentetControll(_ sender: UISegmentedControl) {
value 1
value 2
value 3
}
#IBAction func calculateButton(_ sender: Any) {
if value 1 {
do this
}
else if value 2 {
Do that
}
else if value 3 {
Do thids
}
You need to connect it via #IBOutlet to file.
Then you can use its index to do whatever you want.
#IBOutlet var segmentedControl: UISegmentedControl!
#IBAction func calculateButton(_ sender: Any) {
let index = segmentedControl.selectedSegmentIndex
//...
}
Putting the code in the IBAction method connected via the storyboard or Xib file is the correct approach. You can use strong types when hooking up the action to the control and write code such as:
#IBAction func calculateButton(_ sender: UISegmentedControl) {
switch(sender.selectedSegmentIndex){
case 1:
// Do something
case 2:
// Do something
case 3:
// Do something
default:
break
}
}
You would need to create an #IBOutlet var segmentedControl: UISegmentedControl? property that connects to the segmented control in the Interface Builder, and then in the calculateButton method you can switch over segmentedControl.selectedSegmentIndex. Let me know if you need further explanation.
I need a UISegmentedControl with 2 row, I could not implement it.
I dont want to use 2 different UISegmentedControl. Are there any solution to this problem? How to implement UISegmentedControl with two rows?
'UISegmentedControl' does not support multiple rows. If you need it you'll have to build it yourself or find a third party library that offers it.
As Helium suggests in his/her comment, you could create a custom control that internally manages 2 or more segmented controls and handles changing states between them.
Truth be told a segmented control is not that complicated. It wouldn't be that hard to create a custom 2D segmented control. If this is a key need then you might want to go that route.
Basically, you will need to have an IBAction and IBOutlet on each of your segmented control.
Code the IBAction such that clicking on one of the segmented control will set the IBOutlet's selectedSegmentIndex of the other segmented controls(which are not selected) to UISegmentedControlNoSegment. This creates the illusion that the segmented controls are linked.
Then getting the correct info of which segmented control value is selected is simply gathering all the index of the segmented control into one constant.
#IBOutlet weak var segment1: UISegmentedControl!
#IBOutlet weak var segment2: UISegmentedControl!
#IBOutlet weak var segment3: UISegmentedControl!
#IBAction func FirstSecond(_ sender: UISegmentedControl) {
segment2.selectedSegmentIndex = UISegmentedControlNoSegment
segment3.selectedSegmentIndex = UISegmentedControlNoSegment
}
#IBAction func ThirdFourth(_ sender: UISegmentedControl) {
segment1.selectedSegmentIndex = UISegmentedControlNoSegment
segment3.selectedSegmentIndex = UISegmentedControlNoSegment
}
#IBAction func FifthSixth(_ sender: UISegmentedControl) {
segment1.selectedSegmentIndex = UISegmentedControlNoSegment
segment2.selectedSegmentIndex = UISegmentedControlNoSegment
}
let segmentvalue = ["First","Second","Third","Fourth","Fifth","Sixth"]
#IBAction func FirstSecond(_ sender: UISegmentedControl) {
segment2.selectedSegmentIndex = UISegmentedControlNoSegment
segment3.selectedSegmentIndex = UISegmentedControlNoSegment
let valueINeed = segmentvalue[segment1.selectedSegmentIndex]
}
#IBAction func ThirdFourth(_ sender: UISegmentedControl) {
segment1.selectedSegmentIndex = UISegmentedControlNoSegment
segment3.selectedSegmentIndex = UISegmentedControlNoSegment
let valueINeed = segmentvalue[segment2.selectedSegmentIndex + 2]
}
#IBAction func FifthSixth(_ sender: UISegmentedControl) {
segment1.selectedSegmentIndex = UISegmentedControlNoSegment
segment2.selectedSegmentIndex = UISegmentedControlNoSegment
let valueINeed = segmentvalue[segment3.selectedSegmentIndex + 4]
}
Full Details availabe here: multi row segemented control in swift
My switch doesn't work, when i press the first button which is "buttonAclicked" a label is showing which is also what i wanted but when i try to click on the button with the tag 3 i get a SIGABRT error which crashes the whole thing.
I know that the button with the tag 3 works since i have made an IBAction for itself where it printed out:
">"
but when i set it into the switch statement it does not work, nothing is being printed besides errors and the other prints but not the last print
func buttonAclicked(sender: UIButton) {
print("button A was pressed")
label.hidden = false
label.setNeedsDisplay()
if(sender.isKindOfClass(UIButton)) {
print("hey")
}
switch sender.tag {
case 3 :
print("buuuutttttooonnn")
break
default :
label.text = "0"
}
}
create proper Button IBAction and connect to storyboard proper. it should solve your SIGABRT error.
your error because of not connect IBAction Properly to Storyboard
#IBOutlet weak var buttonOutlat: UIButton! // creat Button Outlet hera
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
buttonOutlat.tag = 3 // <- you can set sender.tag here
}
#IBAction func buttonAclicked(_ sender: UIButton) { // creat proper Button Action
print("button A was pressed")
switch sender.tag{
case 3 :
print("buuuutttttooonnn---3")
label.text = "button-3"
break
default :
label.text = "button-default"
}
}