I have two buttons, how can I change one of the button's image if the other button is tapped? - Swift - swift

So I am making an app where the user will have to categorize their post between two options - "Lost" & "Found". Each button has the option to be clicked, but when I did the Testflight I got the comment that when they clicked the other button because they made a mistake, that the image stayed activated.
Is there a way to change the image of the other button back to the normal deselected image if the other button is clicked?
Here's my code:
var lost = Bool()
#IBOutlet weak var address: UITextField!
#IBOutlet weak var breed: UITextField!
#IBOutlet weak var phone: UITextField!
#IBOutlet weak var imagePosted: UIImageView!
#IBOutlet weak var changeImageLostButton: UIButton!
#IBOutlet weak var changeImageFoundButton: UIButton!
//BUTTONS PRESSED - LOST & FOUND
#IBAction func lostPressedButton(_ sender: UIButton) {
changeImageLostButton.setImage(UIImage(named:"LostButton-active.png"), for: .normal)
lost = true
}
#IBAction func foundPressedButton(_ sender: Any) {
changeImageFoundButton.setImage(UIImage(named:"FoundButton-active.png"), for: .normal)
lost = false
}

The problem seems to be that while you set the image on each button when it is pressed, you don’t remove the image from the other one. All you need to do is remove the image from the appropriate button in the lostPressedButton and foundPressedButton methods:
#IBAction func lostPressedButton(_ sender: UIButton) {
changeImageLostButton.setImage(UIImage(named:"LostButton-active.png"), for: .normal)
// Remove the image on the found button.
changeImageFoundButton.setImage(nil, for: .normal)
lost = true
}
#IBAction func foundPressedButton(_ sender: Any) {
changeImageFoundButton.setImage(UIImage(named:"FoundButton-active.png"), for: .normal)
// Remove the image on the lost button.
changeImageLostButton.setImage(nil, for: .normal)
lost = false
}

Related

printing percentage value into decimal swift

The Result I wantedi have three buttons under same IBAction showing different percentages %0, %10, %20.
and I have another button called calculate in another IBAction. so what I want is. when I choose %10 and press calculate I want to print out the 0.1 in the console. this is very beginner question but I m really stuck here
below is my code
import UIKit
class CalculatorViewController: UIViewController {
#IBOutlet weak var billTextField: UITextField!
#IBOutlet weak var zeroPctButton: UIButton!
#IBOutlet weak var tenPctButton: UIButton!
#IBOutlet weak var twentyPctButton: UIButton!
#IBOutlet weak var splitNumberLabel: UILabel!
#IBAction func tipChanged(_ sender: UIButton) {
zeroPctButton.isSelected = false
tenPctButton.isSelected = false
twentyPctButton.isSelected = false
sender.isSelected = true
}
#IBAction func stepperValueChanged(_ sender: Any) {
}
#IBAction func calculatePressed(_ sender: UIButton) {
}
}
The easiest way is to add the output on the percentage button tapped, e.g. in the #IBAction of the "10%" button:
print("0.1")
If you really need to output on the Calculate button tap, you can first determine which percantage button is selected:
if zeroPctButton.isSelected {
print("0.0")
} else if tenPctButton.isSelected {
// ...
If you don't want to hardcode the output, you need some data to base on. Since you don't show us any model code, we can only rely on the text on the buttons. So, the solution might look something like this (inside the Calculate button's #IBAction):
[zeroPctButton,
tenPctButton,
twentyPctButton]
.filter { $0.isSelected }
.forEach {
print("\(Double(($0.titleLabel.text ?? "").filter { ("0"..."9").contains($0) }) / 100.0)")
}
(It takes the text of the selected percentage buttons, filters out only digits from it, converts the resulting digit string to Double, and divides the result by 100.0.)

How to change Weight property of UIButton title in a custom UITableViewCell?

I have a custom cell that has three buttons, that when one of them is pressed, its title weight changes to bold and the font size of the other two buttons gets shrunked.
As it is shown above, while the titles of the other buttons do change their font size, the weight of the title of the pressed button does not seem to change at all. What am I missing? Thanks.
The code of the custom cell:
import UIKit
class SymptomTableViewCell: UITableViewCell {
#IBOutlet weak var severeButton: UIButton!
#IBOutlet weak var moderateButton: UIButton!
#IBOutlet weak var lowButton: UIButton!
var selectedButton = UIButton()
var buttons = [UIButton]()
override func awakeFromNib() {
super.awakeFromNib()
buttons = [severeButton, moderateButton, lowButton]
}
#IBAction func rateButtonPressed(_ sender: UIButton) {
if sender != selectedButton {
selectedButton = sender
for button in buttons {
if button != selectedButton {
button.titleLabel?.font = .systemFont(ofSize: 12)
}
}
selectedButton.titleLabel?.font = .boldSystemFont(ofSize: 16)
}
}
}
Changing button style from plain to default worked.

If we click on the AddCommentsButton the AddCommentsTextView must enable. how to implement in swift iOS?

#IBOutlet weak var AddCommentsButton: UIButton!
#IBOutlet weak var AddCommentsTextView: UITextView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.AddCommentsButton.setTitle("Add Comments", for: UIControl.State.normal)
self.AddCommentsButton.setTitleColor(UIColor.blue, for: UIControl.State.normal)
AddCommentsTextView.rchDelegate = self as? RCHTextViewDelegate
AddCommentsTextView.delegate = self as? UITextViewDelegate
AddCommentsTextView.placeholder = "Leave a comment"
AddCommentsTextView.canShowBorder = true
AddCommentsTextView.addBorderWithCornerRadius(cornerRadius: 0, borderColor: .Gray, borderWidth: 0.5)
AddCommentsTextView.backgroundColor = UIColor.clear
self.AddCommentsTextView.font = UIFont.dynamicFont(control: .listKeyText)
self.AddCommentsTextView.textColor = UIColor.listKeyTextColor()
}
#IBAction func didTapAddCommentsButton(_ sender: Any) {
}
If you are looking for when you click on didTapAddCommentsButton button then the AddCommentsTextView Textview should show the cursor in the textview for entering the text then you can use:
#IBAction func didTapAddCommentsButton(_ sender: Any) {
self.AddCommentsTextView.becomeFirstResponder()
}
So that on button click it will enable textview cursor for entering purpose.
UPDATED:
If you want to remove focus from the text entry then use:
self.AddCommentsTextView.resignFirstResponder()

So i have a UIImage and I want it so when I click on it, it will change to a different image

#IBOutlet weak var BlueBalloon: UIButton!
#IBOutlet weak var GreenBalloon: UIImageView!
So I've put the image in as a variable, i just want to know where do i go from here?
You need to create #IBAction
#IBAction func buttonClicked(_ sender: UIButton) {
sender.setImage(UIImage(named:"Your image name here"), for: .normal)
}

How to connect outlets and actions in Swift / MacOS programaticly

I have a simple example.
I connected the left button1 and label1 with ctrl-draging it to the contollerclass.
How can I do the same for the right button2 label2 programaticly (without ctrl-draging)
That´s my code:
class ViewController: NSViewController {
#IBOutlet weak var label1: NSTextField! //connected with ctrl-drag
#IBOutlet weak var button1: NSButton! //connected with ctrl-drag
#IBOutlet weak var label2: NSTextField! //not yet connected
#IBOutlet weak var button2: NSButton! //not yet connected
#IBAction func button1Pressed(_ sender: Any) //connected with ctrl-drag
{ label1.stringValue = "button-I"
button1.title = "pressed"
}
#IBAction func button2Pressed(_ sender: Any) //not yet connected
{ label2.stringValue = "button-II"
button2.title = "pressed"
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
If you want to use storyboard and lay out all your elements there, you can't really avoid the ctrl drag (or just drag if you open the connections inspector on the right).
You could however create your second button programmatically in code and not use the storyboard at all for it. Then programmatically add your constraints too.
You can also add the action of the button programmatically (using addTarget) if you would like but that would require at least the IBOutlet to be setup in order to have a reference to the button.