Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
1I am facing a problem in iOS swift. I am trying to hide and show the UIButton text that is added in the UITextview list. I want to hide the button text on click and show on re-click the button. I can't keep nil the button text because i give the background colour to the button according to the alphabet.
[]
Please help me
Thanks in advance
My problem is not solved
Setting your button title color in state normal and selected state
When click to button, just simple change the state.
// Settings
// let button = <your button>
button.setTitleColor(<yourColor>, for: .normal)
button.setTitleColor(UIColor.clear, for: .selected)
// Action
#IBAction func didSelectButton(_ button: UIButton) {
button.isSelected = !button.isSelected
}
As you are saying that you can't nil the button's text, you should do this,
You can implement this Bool extension also,
extension Bool {
mutating func toggle() {
self = !self
}
}
#IBAction func myButton(_ sender: UIButton) {
sender.titleLabel?.isHidden.toggle()
}
this will show and hide your Button's titleLabel text.
UPDATE
#IBAction func btnTapped(_ sender: UIButton) {
sender.isSelected.toggle()
if sender.isSelected == true {
sender.setTitleColor(UIColor.clear, for: .normal)
} else {
sender.setTitleColor(UIColor.blue, for: .normal)
}
}
#IBAction func myButton(_ sender: UIButton) {
if sender.currentTitle = "" {
sender.setTitle("myTitle", for: .normal)
} else {
sender.setTitle("", for: .normal)
}
}
override func viewDidLoad() {
super.viewDidLoad()
myButton.setTitle("myTitle", for: .normal)
myButton.setTitle("", for: .selected)
}
#IBAction func myButtonClicked(_ sender: UIButton) {
myButton.isSelected = !myButton.isSelected
}
Related
I have implemented four checkboxes. Now, I want to select one checkbox at a time and if there's other checkbox selected, I want it deselected at the same time.
This is what I've tried but it selects all the checkboxes.
#IBAction func checkboxTapped(_ sender:UIButton){
if (sender.isSelected == false){
sender.setImage(UIImage(named: "selected"), for: UIControl.State.normal)
sender.isSelected = true;
} else {
sender.setImage(UIImage(named: "unselected"), for: UIControl.State.normal)
sender.isSelected = false;
}
}
Here, I have given same IBAction connection to all the checkboxes.
First create a set up function that sets the corresponding image for the button state.
(All code below was created in a playground so it might be a little different than yours)
private func setUp(button: UIButton) {
button.setImage(UIImage.init(systemName: "checkmark.circle"), for: .selected)
button.setImage(UIImage.init(systemName: "xmark.circle"), for: .normal)
button.addTarget(self, action: #selector(checkboxTapped), for: .touchUpInside) //Not needed if you use a storyboard
}
Then call it from viewDidLoad in your view controller (I have my buttons in an array buttons)
override func viewDidLoad() {
super.viewDidLoad()
buttons.forEach {
setUp(button: $0)
self.view.addSubview($0) //Not needed if you use a storyboard
}
buttons[0].isSelected = true
And then check your action method so it updates all buttons properly
#IBAction func checkboxTapped(_ sender:UIButton){
for button in buttons {
button.isSelected = false
}
sender.isSelected = true
}
If you are not using an array then replace my loops with calls to the individual buttons
I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:
#IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}
However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.
My question is why the image was not changed in button2 after the first click?
What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?
You probably have an issue related to UIButton states that is causing this problem.
I don't think it is a simulator bug.
By the way, a good practice you should follow is to name the outlet different than the #IBAction. Let's say:
#IBAction func buttonTapped(_ sender: Any) {
button.setImage(UIImage(named: "image"), for: .normal)
}
Try this:
override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(named: "image"), for: .selected)
}
#IBAction func buttonTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}
And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.
Rename your method/action so it differs from the button/property.
Change Any to UIButton since you know its class.
#IBAction func buttonTapped(_ buttonTapped: UIButton) {
buttonTapped. button.isSelected = !button.isSelected
}
Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.
it's simulator bug. it worked on a real device
#IBAction func button2(_ sender: UIButton) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}
I have four custom UIButton, I applied button image radio (checked and checked). four button I have separate action method I can change the Image easily but if I checked first button another three button need uncheck. it should react like radio button.
Here, below my code
#IBAction func first_scheme(_ sender: Any) {
bRec = !bRec
if (bRec == true) {
firstscheme_button.setImage(UIImage(named: "uncheck.png"), for: .normal)
} else {
firstscheme_button.setImage(UIImage(named: "check.png"), for: .normal)
}
}
If you have 4 radio buttons at all times, you can put them in an array.
var radioButtons: [ButtonType] = [r1, r2, r3, r4]
You can now access the button in a loop and set the values for the other button to 'unchecked'.
func setRadioButtons(button: ButtonType) {
for radioButton in radioButtons {
if radioButton !== button {
radioButton.setImage(UIImage(named: "uncheck.png"), for: .normal)
}
}
}
#IBAction func first_scheme(_ sender: Any) {
bRec = !bRec
if bRec {
firstscheme_button.setImage(UIImage(named: "uncheck.png"), for: .normal)
} else {
firstscheme_button.setImage(UIImage(named: "check.png"), for: .normal)
}
setRadioButtons(button: sender)
}
alternate method
If all you want to do is check the button clicked and uncheck the other buttons, the logic is simpler.
Create the common action for all radio buttons as well create the IBOutletcollection for your all UIButtons ,
var radioButtons: [UIButton] = [r1, r2, r3, r4]
finally execute the common method as
func setRadioButtons(button: UIButton) {
for getradioButton in radioButtons {
getradioButton.setImage(UIImage(named: "uncheck.png"), for: .normal)
}
button.setImage(UIImage(named: "check.png"), for: .normal)
}
I suggest you set tags on the buttons with the simple tag property, and then you save it from a generic listener.
//first button selected
var lastTag = 0
#IBAction func first_scheme(_ sender: UIButton) {
buttonArray[lastTag].setImage(UIImage(named: "uncheck.png"), for: .normal)
lastTag = sender.tag
sender.setImage(UIImage(named: "check.png"), for: .normal)
}
You Can override isSelected variable in the custom UIButton and set the image depend on the isSelected value.
class customButton: UIButton {
override var isSelected: Bool {
willSet {
self.setImage(UIImage.init(named: "checked"), for: .normal)
}
didSet {
self.setImage(UIImage.init(named: "unchecked"), for: .normal)
}
}
}
After making 4 IBOutlets and 4 IBActions for the four custom UIButtons. you can easily select and unselect the buttons and apply your custom behaviour.
#IBAction func firstButtonAction(_ sender: Any) {
if let button = sender as? customButton {
button.isSelected = !button.isSelected
}
secondButton.isSelected = false
thirdButton.isSelected = false
fourthButton.isSelected = false
}
I want to let user give +1 with one button or -1 points with another button, but they should be able to only press one of these buttons one time...
I use this code, but the user can still click on the button multiple times...
var job: Job! {
didSet {
jobLabel.text = job.text
likeButton.setTitle("👍 \(job.numberOfLikes)", for: [])
dislikeButton.setTitle("👎 \(job.numberOfDislikes)", for: [])
}
}
#IBAction func dislikeDidTouch(_ sender: AnyObject)
{
(dislikeDidTouch).isEnabled = false
job.dislike()
dislikeButton.setTitle("👎 \(job.numberOfDislikes)", for: [])
dislikeButton.setTitleColor(dislikeColor, for: []) }
#IBAction func likeDidTouch(_ sender: AnyObject)
{
sender.userInteractionEnabled=YES;
job.like()
likeButton.setTitle("👍 \(job.numberOfLikes)", for: [])
likeButton.setTitleColor(likeColor, for: [])
}
Since the sender is a UIButton , it's better to construct the funcs like this
#IBAction func dislikeDidTouch(_ sender: UIButton)
#IBAction func likeDidTouch(_ sender: UIButton)
and inside each one do
sender.isEnabled = false
I have code to copy the numbers from the Label.
How it's called?
How can I make that when I click on a button, it appears that on the photo
#IBAction func copybutton(_ sender: UIButton) {
UIPasteboard.general.string = displayResultLabel.text
}
Use this code -
UIPasteboard.general.string = sender.title(for: UIControlState.normal)