Showing Images on Button through IF Statement - ios

I'm new to swift and the iOS platform in general, but I think I have a decent grasp on it.
I'm attempting to make a simple Bingo game, but I've run into a little issue.
I need to give the user the ability to deselect a number. Right now, I've got buttons that the user will click on to to change that piece to marked (giving it a background image). However, I want to change the image to nothing if they press the button again.
For the life of me, I can't seem to figure out how to check, preferably with an IF statement, whether the button has been pressed the first time or not. I was trying to use an IF statement to compare the image with something like this:
#IBAction func action(_ sender: AnyObject) {
if UIImage(named:"MarkedPiece") == true {
// labels for Numbers will be hidden here
} else {
sender.setImage(UIImage(named: "MarkedPiece"), for: UIControlState())
}
}
The above is the function that all of the buttons point to. I get the error message telling me that I can't compare type UIImage to BOOL, which makes sense. Is there another way that I should do it?
Any help would be appreciated!

You can set button image for normal state and selected state.
btn.setImage(UIImage(named: ""), for: .normal)
btn.setImage(UIImage(named: ""), for: .selected)
and use .isSelected
#IBAction func action(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
// labels for Numbers will be hidden here
} else {
}
}

Related

When button pressed, button.setImage(nil) doesn't work

I'm trying to create Japanese crossword. Use buttons as cells. When trying to insert system image, it works (case mode 1). But when I choose other mode (2), the icon doesn't disappear.
let img1 = UIImage(systemName: "multiply"))
#IBAction func cellClicked(_ sender: UIButton) {
for cell in cellButtons {
if cell.tag == sender.tag {
switch typeCell { //when mode changes
case 1:
cell.setImage(img1, for: .normal) //work
default/*2*/:
cell.setImage(nil, for: .normal) //don't work, but running without errors
}
}
}
}
I was trying to use only sender (without if cell.tag == sender.tag; didn't work ), then create IBOutlet collection and run the code above.
Update:
I found and decide to use .isHidden. That works and I also solve my other issue :)
Thanks!

im working on favourites in swift, on favorite_btn click i want to change the image of the button from star to star.fill

I am working on favourites in swift, on favourites button click I want to change the image of the button from star to star.fill which are system images, I put on main story board the star image and in my code I want onClick favoris button to change the image into star.fill, there is my code
#IBAction func add Favoris(_ sender: Any) {
favoris_ button. imageView?.image = UIImage(systemName: "star.fill")
}
But it doesn't work, any help please?
#IBAction func addFavoris(_ sender: Any) {
if #available(iOS 13.0, *) {
favoris_button.setImage(UIImage(systemName: "star.fill"), for: .normal)
} else {
// Fallback on earlier versions
}
}

UIButton not toggling between two images Swift 4.2

I am wanting to add sounds to my app. I have added a UIButton with two images, soundON and soundOFF.
When I call the sound settings in the app the first time, they toggle fine with each image.
However, when I return to the sound settings a second and subsequent time, it is like the soundOff images does not disappear when the soundOn image is displayed.
Odd as the code is so short and simple.
func soundButton() {
sounds = UIButton(frame : CGRect(x: 65, y: 70, width: 40, height:40))
sounds.setImage(UIImage(named : "soundON"), for : .normal)
sounds.setImage(UIImage(named : "soundOFF"), for : .selected)
sounds.showsTouchWhenHighlighted = true
sounds.addTarget(self, action: #selector(soundButtonTapped), for: .touchUpInside)
self.soundView.addSubview(sounds)
}
#objc func soundButtonTapped(_ sender: Any) {
sounds.isSelected.toggle()
isSoundOn.toggle()
}
I have added a video to show the issue as this will save a ton of typing.
http://www.reeflifeapps.com/soundError.mov
Any help is greatly appreciated.
update:
I had the button on a UIView that was hidden on startup of the puzzle. When the user pressed the "Sounds Settings" icon, the sound setting UIView was unhidden. I had the button on this func to unhide the sound settings. I moved it to viewDidLoad() and it fixed it.
I suggest you to define a boolean variable that keeps the current state of your button. Then, you should set current image of button according to the current state of the variable.
var isSoundOn = false
#objc func soundButtonTapped(_ sender: Any) {
isSoundOn.toggle()
if isSoundOn {
// your logic when sound on (set button selected image, action etc)
} else {
// logic when sound off (set button not selected image, action etc)
}
}
If you still want to use soundButton.isSelected as your boolean variable,do not define images for different states in soundButton.setImage(yourImage, for: .selected) and soundButton.setImage(yourImage, for: .normal) and define them as follows:
soundButton.setImage(soundButton.isSelected ? soundOnImage : soundOffImage, for: .normal)
One of those two approaches above can be used.
UPDATE:
As Lloyd Kaijzer stated, isSoundOn = !isSoundOn updated as isSoundOn.toggle()

iOS - Swift 4 . - Updated UIButton label title not being used in function

I have the following code for a button label title (btnTD) which toggles its label title between "L" and "R" AFTER which it is meant to use the UPDATED Label title in a function called by another button's action (btnCalculate in this case).
Both the toggling of the label and the function called by btnCalculate work fine.
However, when I toggle the button, the function is using the button's title label BEFORE it is changed, not AFTER it is changed, even though the UI is showing the title toggling correctly.
It doesn't matter what order I put the toggling of the label or the function called by btnCalculate, the result is always the same.
#IBAction func btnTD(_ sender: UIButton) {
if btnTD.titleLabel!.text! == "R" {
btnTD.setTitle("L", for: .normal)
} else {
btnTD.setTitle("R", for: .normal)
}
btnCalculate.sendActions(for: .touchUpInside)
}
Here is the code for btnCalculate.sendActions(for: .touchUpInside) although I don't think it is relevant
#IBAction func btnCalculate(_ sender: UIButton) {
txtOutput.text = Hold(IBTrk: Int(txtIBT.text!)!, TD: (btnTD.titleLabel!.text!) , OBTime: Double(txtOBTime.text!)!, TAS: Double(Int(txtTAS.text!)!), WD: Int(txtWD.text!)!, WS: Double(txtWS.text!)!)
}
Just use btnTD.currentTitle Not tiltleLabel.Text
#IBAction func btnCalculate(_ sender: UIButton) {
txtOutput.text = Hold(IBTrk: Int(txtIBT.text!)!, TD: (btnTD.currentTitle) , OBTime: Double(txtOBTime.text!)!, TAS: Double(Int(txtTAS.text!)!), WD: Int(txtWD.text!)!, WS: Double(txtWS.text!)!)
}
Setting the button title does so for the .normal state. However the button is being pressed at this time so for a moment (while calculate function is called) its title is still the old value.
Using a global variable in the view controller is one way around this:
// Declare outside a method (in viewController)
var buttonState: String = "L" // Could also be a Bool variable
// Button action
#IBAction func btnTD(_ sender: UIButton) {
if buttonState == "L" {
buttonState = "R"
btnTD.setTitle("R", for: .normal)
} else {
buttonState = "L"
btnTD.setTitle("L", for: .normal)
}
// Then call the calculate method
btnCalculate.sendActions(for: .touchUpInside)
}
Note: btnCalculate.sendActions() might be better as a separate function that is called here and also from btnCalculate button, rather than using sendActions.

Swift - Change color and size of text when UIButton is tapped

Basically, I have a three buttons on my main screen.
When I select one of these buttons, I would like the text on the selected button to change to bold and change color (blue).
When I select a different button, I would like the newly selected button to change to bold and change color(blue), and the previously selected button to go back to normal. (non-bold and black text)
I have these buttons sending an action to the script.
This is what I have, I can't seem to get it to work. Help would be much appreciated!
#IBAction func buttonOne(sender: UIButton){
sender.setTitleColor(UIColor.blueColor(), forState: UIControlState.Highlighted)
}
I have tried .Highlighted and .Selected on the UIControlState, neither seem to work. I have also tried the following, but I cant get it to work.
#IBAction func buttonOne(sender: UIButton){
sender.titleLabel?.textColor = UIColor.blueColor()
}
I figured that since the sender was a UIButton, and it was the button that was clicked, taking the values off of it and resetting them would work. I do believe I am missing something.
Thank you
Sounds like you want UIControlState.Normal
Selected does nothing in most cases, and Highlighted is only while you're pressing the button.
See more info here: https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/index.html#//apple_ref/doc/constant_group/Control_State
Maybe you can do with a transform...
#IBAction func buttonPressed(sender: UIButton) {
sender.titleLabel!.textColor = UIColor.blueColor()
sender.transform = CGAffineTransformMakeScale(0.8, 0.8)
}
#IBAction func buttonReleased(sender: UIButton) {
sender.titleLabel!.textColor = UIColor.redColor()
sender.transform = CGAffineTransformIdentity
}
Provide tags to all buttons, connect each button actions to same function and try the following code:
#IBAction func butnClicked (sender : UIButton) {
for tg in 1...2 {
print(sender.tag)
let tmpButton = self.view.viewWithTag(tg) as? UIButton
if tmpButton?.tag == sender.tag {
tmpButton?.setTitleColor(.red, for: .normal)
} else {
tmpButton?.setTitleColor(.gray, for: .normal)
}
}
Hope will be helping.

Resources