Update Button Title - ios

I have a simple button which is initially labelled with the emoji šŸ¦, and all that I'm trying to do is to remove the emoji once the button is clicked.
import UIKit
class ViewController: UIViewController {
#IBAction func touchCard(_ sender: UIButton) {
flipCard(withEmoji: "šŸ¦", on: sender)
}
func flipCard(withEmoji emoji: String, on button:UIButton){
if button.currentTitle == emoji {
button.setTitle("", for: UIControl.State.normal)
print("Removed emoji")
}
}
}
As I step through the code, the statement button.setTitle("", for: UIControl.State.normal) gets executed, however the emoji does not disappear although it does appear faded, after the button is clicked.
Edit: The title does get updated, but it takes a few (8-10) seconds to do so. Replacing the emoji with another emoji is almost instantaneous though! What could be causing this and how do I fix this?
PS: I'm following along the CS193P lecture (Lecture 1) here.

You probably want button.title(for: .normal) instead of button.currentTitle.

If the button appears faded, it might be disabled. If you set the title for the disabled state and execute button.setTitle("", for: UIControl.State.normal), nothing will happen to the title for UIControl.State.disabled.
Check if button.setTitle("", for: UIControl.State.disabled) does the trick.

You can simply replace your function like below function that i have added for you.
func flipCard(withEmoji emoji: String, on button:UIButton){
if button.currentTitle == emoji {
button.setTitle("", for: .normal)
button.setTitle("", for: .selected)
button.setTitle("", for: .disabled)
print("Removed emoji")
}
}
Hope this way may help you.

Related

Showing Images on Button through IF Statement

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 {
}
}

Cannot find method for iOS Native Development

I am going through a tutorial where a button and label is kept and to change the label text on click of button a method is called in tutorial
sender.titleForState(.Normal)
But in my Xcode, I cannot find the same method.
If you are looking to change title of a UIButton in Swift 3/4
sender.setTitle("Your title", for: .normal)
Swift 3
try this
func buttonPressed(_ sender: UIButton) {
sender.setTitle("Your title", for: .normal)
}
And call this func by adding a selector like this
youbutton.addTarget(self, action: #selector(YourController.buttonPressed(_:)), for: .touchUpInside)
Add this to your viewDidLoad() method
Hope it helps.
If you want to change button's title by click on button then you need to write code in button'e action method
Ex.
#IBAction func clickOnButtonForChangeTheTitle(_ sender: UIButton)
{
sender.setTitle("I'm changing the title", for: .normal)
}

Remove button when click itselt in swift 3

Hello I have a custom UIButton has added. And I want to remove this button when click on it self. I have done like this.
btnDelete.addTarget(self, action: #selector(deleteCoveringPerson(sender:)), for: .touchUpInside)
btnDelete.setImage(UIImage.init(named: "close-dark"), for: .normal)
btnCoveringPerson.addSubview(btnDelete)
And this is my delete button selector
func deleteCoveringPerson(sender:UIButton)
{
dm.strCoveringPersonNAme=""
dm.strcoveringPersonCode="0"
btnCoveringPerson.setTitle(lan.getConvertedLanguageString(word: "COVERINGPERSON"), for: .normal)
btnDelete.removeFromSuperview()
}
How can I do this?
For me this work just fine
func deleteCoveringPerson(sender:UIButton)
{
dm.strCoveringPersonNAme=""
dm.strcoveringPersonCode="0"
btnCoveringPerson.setTitle(lan.getConvertedLanguageString(word: "COVERINGPERSON"), for: .normal)
sender.removeFromSuperview()
}
Hope this helps

Button Wonā€™t Change Image On-Click After User Goes to Home Screen

I have a 5 different buttons where I want the user to be able to select one if itā€™s not selected, and de-select it if it already is selected, while at the same time de-selecting another button if it is selected.
normal = buttonā€™s image when not selected filled = buttonā€™s image when selected
To do this I have created an if/else statement:
#IBAction func option1ButtonAction(sender: AnyObject) {
if (option1Button.currentImage == UIImage(named: "normal")) {
if option2Button.currentImage == UIImage(named: "filled") || option3Button.currentImage == UIImage(named: "filled") || option4Button.currentImage == UIImage(named: "filled") || option5Button.currentImage == UIImage(named: "filled") {
option1Button.setImage(filled, forState: .Normal)
option2Button.setImage(normal, forState: .Normal)
option3Button.setImage(normal, forState: .Normal)
option4Button.setImage(normal, forState: .Normal)
option5Button.setImage(normal, forState: .Normal)
}
else {
option1Button.setImage(filled, forState: .Normal)
}
}
else {
option1Button.setImage(normal, forState: .Normal)
}
}
Afterwards I made an if option1Button.currentImage == UIImage(named: "filledā€) statement. Everything works exactly how I want it to, however I have one problem. Whenever the user presses the home button and then goes right back into the app, the buttons still have the ā€œnormalā€ image, even when clicked.
Inside the viewDidDisappear function I put the following code in hopes to fix this issue:
option1Button.setImage(normal, forState: .Normal)
option2Button.setImage(normal, forState: .Normal)
option3Button.setImage(normal, forState: .Normal)
option4Button.setImage(normal, forState: .Normal)
option5Button.setImage(normal, forState: .Normal)
But I still have the problem occurring. I would be grateful for any help provided.
You need change image from your button when clicked! I understand right?
For your issue I believe you can do different. I make a code for example.
First set image for state in button.
option1Button.setImage(normal, forState: UIControlState.Normal);
option1Button.setImage(filled, forState: UIControlState.Disabled);
option2Button.setImage(normal, forState: UIControlState.Normal);
option2Button.setImage(filled, forState: UIControlState.Disabled);
Second I made a function, this function change state of your button.
selectButton(button:UIButton)
{
option1Button.enabled = button != option1Button;
option2Button.enabled = button != option2Button;
}
Now you just do implement this way you action, and other detail is you don't need more create a function for every button ex. #IBAction func option1ButtonAction(sender: AnyObject) only create one action #IBAction func onButtonClicked(sender: AnyObject) and like all buttons here... remember only increment your buttons in your selectButton(button:UIButton) function, or if preferrer you can create a collection and push your button there and do a while in selectButton
#IBAction func onButtonClicked(sender: AnyObject) {
if let clickedButton = sender as? UIButton{
selectButton(clickedButton);
}
}
If I understood correctly your issue, I hope helped you!

How to change button text in Swift Xcode 6?

Here's what I'm trying to do. If you've ever played Halo or CoD, you'd know that you could change the name of a weapon load-out.
What I'm doing is making it so you can change your load-out name using a text field. Here's the problem, the load-out name in the load-out menu is a button (to select and view info about that load-out) and I could just write this:
#IBAction func renameClassButton(sender: AnyObject) {
classTopButton.text = "\(classTopTextField)"
}
Except it [classTopButton] is a button which doesn't allow the '.text' suffix
You can do:
button.setTitle("my text here", forState: .normal)
Swift 3, 4, and 5:
button.setTitle("my text here", for: .normal)
In Xcode 8 - Swift 3:
button.setTitle( "entertext" , for: .normal )
It is now this For swift 3,
let button = (sender as AnyObject)
button.setTitle("Your text", for: .normal)
(The constant declaration of the variable is not necessary just make sure you use the sender from the button like this) :
(sender as AnyObject).setTitle("Your text", for: .normal)
Remember this is used inside the IBAction of your button.
NOTE:
line
someButton.setTitle("New Title", forState: .normal)
works only when Title type is Plain.
swift 4 work as well as 3
libero.setTitle("---", for: .normal)
where libero is a uibutton
You can Use sender argument
#IBAction func TickToeButtonClick(sender: AnyObject) {
sender.setTitle("my text here", forState: .normal)
}
In Swift 4 I tried all of this previously, but runs only:
#IBAction func myButton(sender: AnyObject) {
sender.setTitle("This is example text one", for:[])
sender.setTitle("This is example text two", for: .normal)
}
Note that if you're using NSButton there is no setTitle func, instead, it's a property.
#IBOutlet weak var classToButton: NSButton!
. . .
classToButton.title = "Some Text"

Resources