I have implemented this code for setting images of the button to implement checkbox using didSet function.
But now I don't want to use didSet function.
Please suggest me other way to implement the same code without using didSet.
//changing image using didSet
didSet
{
if (self.isChecked == true)
{
self.setImage(checked, forState: .Normal)
}
else
{
self.setImage(unChecked, forState: .Normal)
}
}
You can use a UISwitch with custom images set in the storyboard, then you just read the value
Related
I'm making simple quiz app about countries in Europe, I have a map and three buttons below with names of countries, one of them is correct and of course two incorrect. I want to highlight incorrect (if user click incorrect button) button for red and correct for green, if user click correct button I want to highlight it for green and after maybe 5s come back to the same color that was at first. I know how to change button color but I don't know ho do that for 5s and come back to default color. How can I do that ? Below its code that I use to change button color
UIButtonOutlet.Backgroundcolor.Uicolor.green
But its default green, so I can't set my color.
You can try something like this. First declare one UIButton instance in your class and then scheduledTimer after setting backgroundColor of button for correct's and incorrect and store that button reference with button that you have created first.
var selectedButton = UIButton()
Now use this selectedButton when you are setting button's backgroundcolor.
btnOutlet.backgroundcolor = .green //For correct
btnOutlet.backgroundcolor = .red //For incorrect
self.selectedButton = btnOutlet
//Now scheduled the timer for 5.0 sec
Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(setButtonBGBack), userInfo: nil, repeats: false)
Add the setButtonBGBack method in your class
func setButtonBGBack() {
self.selectedButton.backgroundcolor = .blue //Set to default here
self.selectedButton = UIButton()
}
Lets say you have your code for changing the color which is in the "changeColor" func:
var correctButton: UIButton
func changeColor() {
if correctButton.backgroundColor == .green {
correctButton.backgroundColor = .lightGray //back to original color
}
}
What this essentially does is changes your correct button color back to its default (whatever that may be) if it is green when the func is called.
Now to use this, we can do a little work inside of the IBAction that is connected to each of your buttons:
#IBAction func buttonClicked(sender: UIButton) {
if sender.titleLabel?.text == "Correct Answer" {
button.backgroundColor = .green
correctButton = button //set the correct button variable so the changeColor func can be used
let timer = Timer.init(timeInterval: 5, target: self, selector: #selector(changeColor), userInfo: nil, repeats: false)
} else if sender.titleLabel?.text == "False Answer 1" || sender.titleLabel?.text == "False Answer 2" {
button.backgroundColor = .red
}
}
So in this code, if you click the button that is the correct answer (you can identify this by tag or other means, but I just decided to use its text) then the color is set to green immediately, and then the correctButton variable is set, but then a timer is initiated that after five seconds will call your changeColor func and then change it back to its original color.
Hope this helps :)
Edit:
Of course, my method assumes that you are using the storyboard to create these buttons. If you are creating them programmatically, then NiravD's method will work better.
I have one UIButton in a UIView and I change its title. However, when I get the button title it is giving me the old title and not the changed one. However, when I call it from somewhere when the method is finished it's true
Thanks,
Wrapping your if statements in a performWithoutAnimation block will prevent any delays in updates to the titleLabel.
UIView.performWithoutAnimation {
if btnOne.titleLabel?.text == "qq" {
btnOne.setTitle("metin", forState: .Normal)
var a = btnOne.titleLabel?.text // "metin"
mergeString += sender.titleLabel!.text!
}
...
}
I've got custom buttons on a custom tableview cell, when button is pressed the image changes to checked/unchecked (see code below).
Issues is: How do I add the cell row data thats checked to an array that will be its own table (eg. a table with only checked data).
#IBAction func tickAction(sender: UIButton) {
println("SSSS")
if (sender.selected) {
sender.setImage(UIImage(named:"Unchecked.png"), forState: .Normal)
sender.selected = false
}
else {
sender.setImage(UIImage(named:"Checked.png"), forState: .Normal)
sender.selected = true
}
}
A solution would be to extend UITableViewCell into your own class that holds the other data or holds a reference to a model that holds the data.
Ideally, the view controller should hold the models and should get notified by a view that the action took place to act accordingly. The view controller should then add the data to the other array.
I have 16 buttons named like btn1, btn2 etc.. how can i loop through this and set an value to each button?
I have this code:
//set values on the button
for i in 1..<17{
var mybutton = "btn" + String(i);
println(mybutton)
mybutton.setTitle(String(number), forState: UIControlState.Normal)
}
}
but that doesn't work because the software sees it as an string...
How can i solve this issue?
You can create array of buttons:
let array = [btn1, btn2, btn3 ...]
for i in array.count
{
//do something
array[i].setTitle(String(number), forState: UIControlState.Normal)
}
But if you have a lot of buttons in Interface Builder, the better way to create them programmatically.
In addition to the answer of ChikabuZ:
If you separately added the buttons in storyboard, I suggest you to create an outlet collection instead: #IBOutlet var buttons: [UIButton]!.
CTRL drag all those buttons in this outlet and you can access the array like ChikabuZ has suggested.
I personally prefer to use a map instead:
buttons.map { $0.setTitle(String(number), forState: .Normal) }
If you added these in the interface builder and have a reference to the superview, you can assign each of them a tag from 1 to 16 access them using the method "viewWithTag":
//set values on the button
for i in 1..<17{
var sView = [REFERENCE TO YOUR SUPERVIEW HERE]
var mybutton = sView.viewWithTag(i) as! UIButton?
println(mybutton)
mybutton.setTitle(String(number), forState: UIControlState.Normal)
}
}
I need to change the color of my button's text.
I also need to change the state to Disabled after the user presses it.
I have no idea how to do this. I've been looking things up for a while but they're all either in objective C or I can't understand it (usually help docs, they're stupid.).
In swift you change color for a specific State with the setTitleColor method.
In you case it will be :
button.setTitleColor(UIColor.grayColor, forState: UIControlState.Normal)
Swift 5 Update:
button.setTitleColor(UIColor.grayColor, for: UIControl.State.normal)
To change color of text
button.titleLabel.textColor = UIColor.grayColor()
To change state, on button press add following -
button.enabled = true
IBAction method should be like -
#IBAction func buttonTapped(sender : UIButton!) {
sender.enabled = false
}
Swift 3
button.setTitleColor(UIColor.gray, for: UIControlState.normal)
Note that;
grayColor has been renamed to gray
Normal is now normal (lowercase)
You have to set the text colour for the specific button state.
For Swift3, try below code :
#IBAction func butnClicked(sender : UIButton) {
sender.setTitleColor(.red, for: .normal)
sender.isEnabled = false
}
Set Enabled and text color from story board.