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)
}
Related
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.
How can one program a button that executes a set of commands when it is pressed and then stops the execution when it is released in Xcode? For example: when the button is pressed a light is turned on and when it is released the light turns off.
It is pretty simple using IBActions/Targets:
let btn = UIButton()
btn.addTarget(self, action: #selector(self.on(_:)), for: .touchDown)
btn.addTarget(self, action: #selector(self.off(_:)), for: .touchUpInside)
You could also use storyboards to give the same effect
#IBAction func on(_ sender: UIButton?) -> Void {}
#IBAction func off(_ sender: UIButton?) -> Void {}
Then when connecting your actions, connect on() for your onTouchDown and off() for your onTouchUpInside, etc..
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.
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"
Simple question here. I have a UIButton, currencySelector, and I want to programmatically change the text. Here's what I have:
currencySelector.text = "foobar"
Xcode gives me the error "Expected Declaration". What am I doing wrong, and how can I make the button's text change?
In Swift 3, 4, 5:
button.setTitle("Button Title", for: .normal)
Otherwise:
button.setTitle("Button Title", forState: UIControlState.Normal)
Also an #IBOutlet has to declared for the button.
Just a clarification for those new to Swift and iOS programming. Below line of code:
button.setTitle("myTitle", forState: UIControlState.Normal)
only applies to IBOutlets, not IBActions.
So, if your app is using a button as a function to execute some code, say playing music, and you want to change the title from Play to Pause based on a toggle variable, you need to also create an IBOutlet for that button.
If you try to use button.setTitle against an IBAction you will get an error. Its obvious once you know it, but for the noobs (we all were) this is a helpful tip.
Swift 5.0
// Standard State
myButton.setTitle("Title", for: .normal)
Swift 5:
let controlStates: Array<UIControl.State> = [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved]
for controlState in controlStates {
button.setTitle(NSLocalizedString("Title", comment: ""), for: controlState)
}
Swift 3:
Set button title:
//for normal state:
my_btn.setTitle("Button Title", for: .normal)
// For highlighted state:
my_btn.setTitle("Button Title2", for: .highlighted)
Changing title when attributed is a bit different :
I just ran into a problem : If you have an UIButton with an Attributed Title, you have to use :
my_btn.setAttributedTitle(NSAttributedString(string: my_title), for: my_state)
as, per Apple SetTitle Doc :
If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.
I had an attributed title and I tried to setTitle on it, with no effect...
Swift 3
When you make the #IBAction:
#IBAction func btnAction(_ sender: UIButton) {
sender.setTitle("string goes here", for: .normal)
}
This sets the sender as UIButton (instead of Any) so it targets the btnAction as a UIButton
As of 12/12/2021 - Swift version 5.5.1^ assuming you already have an IBOutlet linked to yourButton in a normal state.
yourButton.setTitle("Title of your button", for: .normal)
swift 4.2 and above
using button's IBOutlet
btnOutlet.setTitle("New Title", for: .normal)
using button's IBAction
#IBAction func btnAction(_ sender: UIButton) {
sender.setTitle("New Title", for: .normal)
}
Swift 3
let button: UIButton = UIButton()
button.frame = CGRect.init(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100)
button.setTitle(“Title Button”, for: .normal)
To set a title for a button in Xcode using swift - 04:
first create a method called setTitle with parameter title and UIController state like below ;
func setTitle(_ title : String?, for state : UIControl.State) {
}
and recall this method in your button action method
like ;
yourButtonName.setTitle("String", for: .state)