I'm trying to realize tik tak toe game. So, I have 9 buttons and every time I'm pressing on them they install their text label as "X" or "O"
sender.setTitle("X", for: .normal)
// or
sender.setTitle("O", for: .normal)
But then , when game is finished , I want to delete all text labels and facing a problem - I can't remove text labels. I've tried several variants and still can't understand problem. I tried :
button.setTitle(nil, for: .normal)
button.setTitle("", for: .normal)
button.titleLabel?.text = ""
button.titleLabel?.text = nil
It's not working. Even if I don't see text at this buttons after my "failed reset", text is still set.
Even when I'm doing all variants to delete text and then calling
button.titleLabel?.text
Im getting not empty line or nil, Im getting "X"!!! (if there was "x" text before)
i checked it ... these all lines work .. problem is with your connection
#IBOutlet weak var button: UIButton!
override func viewDidLoad() {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.button.setTitle("", for: .normal)
//self.button.backgroundColor = .red
}
super.viewDidLoad()
// Do any additional setup after loading the view.
}
Related
Before I upgraded to Swift 4.2 - Xcode 10.1 the DLRadioButton I used had an even spacing between the icon and the title. I never set the spacing and everything worked fine. After the upgrade the icon and the title overlaps
The cocoapod for it says that it uses a default marginWidth of kdefaultmarginwidth
I tried to set the marginWidth in code to anything that would definitely add spacing like 50.0 but the overlap stays. I read somewhere that the kdefaultmarginwidth spacing is 5.0
How can I fix the spacing?
code:
let saleButton: DLRadioButton = {
let button = DLRadioButton(type: .custom)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Sale", for: .normal)
button.setTitleColor(UIColor.lightGray, for: .normal)
button.marginWidth = 50.0 // I tried 5.0, 10.0, 20.0, even 100.0 but nothing
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(saleButton)
// constraints get set
}
This is a choppy fix but it works for now because everything else I tried didn't work. Inside the saleButton closure, I had to add 4 empty spaces before I set the string for the title:
I changed this:
button.setTitle("Sale", for: .normal)
to this and the overlapping is now gone
// there are 4 empty spaces in front of the word Sale
button.setTitle(" Sale", for: .normal)
Here is the image below:
I have 2 button like picture below, their text label are: "Input location...". Now i want to click on the swap button on the right, so the text label of these button will swap. It's look like you have 2 location and you want to swap it. Please any one can point me how to do it. Thank you very much.
On click just set their text to each other.
var tempString = secondButton.titleLabel.text
secondButton.setTitle(firstButton.titleLabel.text, forState: UIControlState.Normal)
firstButton.setTitle(tempString, forState: UIControlState.Normal)
#IBOutlet var firstButton: UIButton!
#IBOutlet var secondButton: UIButton!
#IBAction func didTouchUpInsideSwapButton() {
let firstButtonText = firstButton.titleLabel?.text
firstButton.setTitle(secondButton.titleLabel?.text, for: .normal)
secondButton.setTitle(firstButtonText, for: .normal)
}
I have multiple buttons that all get their titles from an array. I would like to be able to assign the title with a loop, but I can't figure how to refer to each button as I go through the loop.
Currently I am adding each title with a line of code like this:
button0.setTitle(title[0], forState: .Normal)
button1.setTitle(title[1], forState: .Normal)
button2.setTitle(title[2], forState: .Normal)
button3.setTitle(title[3], forState: .Normal)
etc...
I have added an IBOutlet to each button, but I am also using tags for another purpose, so if there is a way to use tags to assign the titles, I would be happy to do that.
Any thoughts?
You need an IBOutletCollection
In your Swift ViewController, assign all your buttons to the below
#IBOutlet var buttons: [UIButton]!
Then to assign the titles
var buttonTitles = ["Button1","Button2"]
for (index,button) in buttons.enumerate()
{
if buttonTitles.count > index
{
if let title : String = buttonTitles[index]
{
button.setTitle(title, forState: .Normal)
}
}
}
When I first run my app, I retrieve a number from my server and display it for my UIButton label. Think of this as a notification number displayed on a red UIButton.
When I remove a notification within the app, I want my UIButton label decrement by 1. I am able to get the decremented number from the server after I delete a notification, but I can't display this new number on the UIButton. The button always displays the number when the app is first fired.
I call makeButtonView() method after I remove a notification to update the UIButton
func makeButtonView(){
var button = makeButton()
view.addSubView(button)
button.tag = 2
if (view.viewWithTag(2) != nil) {
view.viewWithTag(2)?.removeFromSuperview()
var updatedButton = makeButton()
view.addSubview(updatedButton)
}else{
println("No button found with tag 2")
}
}
func makeButton() -> UIButton{
let button = UIButton(frame: CGRectMake(50, 5, 60, 40))
button.setBackgroundImage(UIImage(named: "redBubbleButton"), forState: .Normal)
API.getNotificationCount(userID) {
data, error in
button.setTitle("\(data)", forState: UIControlState.Normal)
}
button.addTarget(self, action: "targetController:", forControlEvents: UIControlEvents.TouchUpInside)
return button
}
Use this code for Swift 4 or 5
button.setTitle("Click Me", for: .normal)
I need more information to give you a proper code. But this approach should work:
lazy var button : UIButton = {
let button = UIButton(frame: CGRectMake(50, 5, 60, 40))
button.setBackgroundImage(UIImage(named: "redBubbleButton"), forState: .Normal)
button.addTarget(self, action: "targetController:", forControlEvents: UIControlEvents.TouchUpInside)
return button
}()
func makeButtonView(){
// This should be called just once!!
// Likely you should call this method from viewDidLoad()
self.view.addSubview(button)
}
func updateButton(){
API.getNotificationCount(userID) {
data, error in
// be sure this is call in the main thread!!
button.setTitle("\(data)", forState: UIControlState.Normal)
}
}
There have been some updates since Swift 4. This works for me:
self.button.setTitle("Button Title", for: UIControl.State.init(rawValue: 0))
Replace button with your IBOutlet name. You can also use a variable or array in place of the quoted text.
It's fairly simple ...
import UIKit
class ViewController: UIViewController {
#IBOutlet var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.setTitle("hello world", forState: UIControlState.Normal)
}
}
I believe if you set the state to normal, the value will propagate by default to other states so long as you haven't explicitly set a title for those states.
Said differently, if you set it for normal, it should also display this title when the button enters additional states
UIControlState.allZeros
UIControlState.Application
UIControlState.Disabled
UIControlState.Highlighted
UIControlState.Reserved
UIControlState.Selected
Lastly, here's Apple's documentation in case you have other questions.
Since your API call should be running on a background thread you need to dispatch your UI update back to the main thread like this:
DispatchQueue.main.async {
button.setTitle(“new value”, forState: .normal)
}
After setting the title, just a simple redraw of the button will do:
button.setNeedsDisplay();
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"