UIButton text content keeps resetting every second update - ios

I am trying to update a button with a test value and I have noticed that every second update the button title text shows the test value for a fraction of a second but then resets to the Button's default value.
It seems to be a bug, but I wanted to see if there is a simpler explanation.
I have tried waiting up to 10 seconds before pushing the button but this seems to be consistently occurring.
Any ideas how to make UIButton function as expected?
import UIKit
class ViewController: UIViewController {
var testEntry = "its working"
#IBOutlet weak var testButton: UIButton!
#IBOutlet weak var testLabel: UILabel!
#IBAction func runTest(sender:
UIButton) {
// The button value should equal the value of the label value, but every 2nd button press of the test button results in the title of the button value resetting to the default value
dispatch_async(dispatch_get_main_queue()) {
self.testLabel.text = "\(self.testEntry)"
self.testButton.titleLabel?.text = "\(self.testEntry)"
}
}
Here is the github project.

You shouldn't be directly setting the text of the button title label, you should only set the font directly onto the label. The text should be set by calling
func setTitle(_ title: String?, forState state: UIControlState)
The text toggles because you're selecting and de-selecting the button, which is switching between some of its states which have different titles.

You should set the button title text by using the following method...
self.testButton.setTitle(self.testEntry, forState: .Normal)
instead of titleLabel property.

Swift 5
self.testButton.setTitle("hello", for: .normal)

Related

Setting the background of an UIButton programmatically not working

I'm just trying to simply set the background image of the button, but whatever I try it is doing nothing. I also put a UIImageView there to see if there's any issue with the image file, but the image view is set perfectly fine. Do I have to set something in the properties of the button?
Here the code:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageToSet = UIImage(named: "3-1")
b1.setBackgroundImage(imageToSet, for: .normal)
imageView.image = imageToSet
}
#IBOutlet weak var b1: UIButton!
#IBOutlet weak var imageView: UIImageView!
}
These are my setting for the button:
In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on (If you set one of them 4).So when you try to use old behaviours like
setBackgroundImage(..)
setImage(..)
This functions not gonna effect on this button. So If you want to use old behaviours you need to set style Plain to Default

segue text on button with action (swift3)

I would like the user to hit the button add 1. Which then will display in the middle of the green box. So the user hits a button and the number one is displayed in the middle of the green box. This is not a counter so its just one. Think of it as a score card. The green box is referenced as score display.
import UIKit
class ViewController: UIViewController {
#IBOutlet var scoreDisplay: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func add1(_ sender: Any) {
}}
If I understood correctly, you wish to hide and show some views depending upon some user behaviour.
As of Swift 3, please take a look at the isHidden property of any UIView. You must have some #IBOutlets for your buttons, not only #IBActions.
A possible solution for you (write this in your add1 function):
scoreDisplay.setTitle("1", for: .normal)
scoreDisplay.isHidden = false
Of course, do not forget to firstly set the scoreDisplay as hidden in the Interface Builder.

Why does my UIButton affect my UIImageView in Swift?

I'm building a simple app in Swift, and the app contains a png image, a UISegmentedControl, and a UIButton. Selecting different controls on the UISegmentedControl resizes an image of a pizza. When I click on "Large" in the UISegmentedControl, and then I press a button on the interface called submitOrderDisplay, it shrinks the image from its large size to its small size. I know it has something to do with the setTitle method I am using at the very bottom of the code, but I don't understand why that causes the image to reset to its default small size. If I remove the setTitle method, the resizing does not occur after I click the UIButton. I don't want the image to change size after the button is pressed. How do I achieve that?
import UIKit
class SecondViewController: UIViewController{
#IBOutlet weak var sizeChoiceControl: UISegmentedControl!
#IBOutlet weak var submitOrderDisplay: UIButton!
#IBOutlet weak var myImageView: UIImageView!
#IBAction func sizeSelected() {
if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Small"){
myImageView.setHeight(height: 85)
myImageView.setWidth(width: 85)
myImageView.setX(x: 146)
myImageView.setY(y: 63)
}
else if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Medium"){
myImageView.setHeight(height: 100)
myImageView.setWidth(width: 100)
myImageView.setX(x: 139)
myImageView.setY(y: 55)
}
else if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Large"){
myImageView.setHeight(height: 115)
myImageView.setWidth(width: 115)
myImageView.setX(x: 131)
myImageView.setY(y: 48)
}
}
#IBAction func SubmitOrderClicked(sender: UIButton) {
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Normal)
}
}
Write the configuration of the button inside the viewDidLoad function:
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Normal)
I understand that you want to put the title Update Order.
If you want to change the title when pressed, you must call it for another State of the button like:
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Highlihgted)
If you change the configuration of the button for different states when an event happens, that must be why the size is ressetting.

Xcode how to connect view to view using button ? (swift)

I'm making a custom keyboard using swift on Xcode.
I have created some views(xib files) which are of keyboards.
and I wanna connect these views each other like when I press [123]button, the view changes to the numeric one so I can type numbers :)
Actually I finished globe button(which is next keyboard button which is auto-created when I added custom keyboard target) but can't understand the codes ;-;
What I want to do is to connect button(as an IBOutlet or IBAction) with view(xib)
Take two views in xib or storyboard and IBOutlet them. One view has the buttons for alphabets and other view has buttons for numbers.
You have to do code something like below :
#IBOutlet var alphabelView: UIView!
#IBOutlet var numericView: UIView!
#IBOutlet var toggleBtn: UIButton!
btn.setTitle("ABC", forState: .Normal)
btn.setTitle("123", forState: .Selected)
toggleBtn.addTarget(self, action: #selector(self.toggleButtonsClicked(_:)), forControlEvents: .TouchUpInside)
Numeric view will be hidden initially. When user press on 123 do code like below lines of code:
func toggleButtonsClicked(button: UIButton) {
button.selected = !butto.selected
alphabelView.hidden = true
numericView.hidden = false
}

Implementing UIButtons for selection in Swift

I'm new to iOS development and I'm working on a Fire Safety App. I would like to give the user the option of 3 risk levels: Low, Medium and High by using (I suppose) 3 UIButtons which the user can tap, and which ever one they tap would turn a certain colour.
I've managed to get 3 UIButtons working, and they can change colour when tapped but if I tap another I can't get the previous button to change back and I've had difficulties getting a value for them (For example when submitted if the user pressed Low I would use the number 2 for calculations)
To show this I did a quick drawing:
Button Example
Thanks for your help :)
==== EDIT ====
I am now using a Segmented Control instead of UIButtons to do this. But I need to know how to use them in an if statement
#IBOutlet var RiskChoice: UISegmentedControl!
#IBOutlet var ValueLabel: UILabel!
#IBOutlet var CalcButton: UIButton!
#IBAction func Calculate(sender: UIButton) {
if (RiskChoice.selectedSegmentIndex == 0){
ValueLabel.text = String("Low")
}
Nothing appears in the Label I have set up.. Could someone direct me here?
You can just reset all the 3 buttons to their default state when any of the button is tapped and then highlight the current button.
#IBAction func buttonClicked(sender: UIButton) {
/** Reset all button to default state*/
resetAllButtons()
/** Highlight current button */
sender.backgroundColor = UIColor.redColor()
sender.titleColor = UIColor.whiteColor()
}
private func resetAllButtons() {
button1.backgroundColor = UIColor.clearColor()
button1.titleColor = UIColor.blackColor() /// Or use this : button1.setTitleColor(UIColor.blackColor, forState: .Normal)
button2.backgroundColor = UIColor.clearColor()
button2.titleColor = UIColor.blackColor()
button3.backgroundColor = UIColor.clearColor()
button3.titleColor = UIColor.blackColor()
}

Resources