How to loop through buttons - swift

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

Related

create a UIButton for each object in array

Hello guys I want to create a UIButton programmatically from every object in an array. something like
for answer in answers {
//create button
}
But how do I assign all these buttons a randomized and unique variable?
like var randomvar = UIButton() ?
I had this problem when I was new to OOP x).
Basically a variable when handling an object is just a pointer to the address in memory.
An array with objects will actually be an array with pointers to all unique objects, in your case these objects are UIButton's.
So in your for-loop you have to add those buttons inside another array.
var buttons = [UIButton]()
for answer in answers {
let btn = UIButton()
...
buttons.append(btn)
}
Then you can access each unique button by accessing them trough buttons.
buttons[2].setTitle("test 2", forControlEvent: .Normal)
Loop over your array and create the button for each item. Either add it to your view directly or save it into an array for later access
var buttons = [UIButton]()
for answers in answer {
let button = UIButton(...)
button.addTarget(self, action: "buttonAction:", forControlEvents: . TouchUpInside)
// add to a view
button.frame = ...
view.addSubview(button)
// or save for later use
buttons.append(button)
}
The better approach is to create an array property:
var array: [UIButton]?
and inside the loop create a button and add it to this array.
If you need to access any button you can access it by index.

How to add to an array when button is pressed?

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.

UIButton setTitle doesn't work

I'm trying to change the text of a UIButton when it gets clicked. I've tried the following things, which failed:
First I tried inserting a button on the storyboard and linking it to the view controller, with the code for the button appearing like this:
#IBAction func button(sender: AnyObject) {}.
Within the button's parentheses, I then used
button.setTitle ("something" , for State: .Selected)
However, when I run this code in the simulator, it doesn't seem to work at all.
I have also tried, following the Apple reference manual, to set different labels for different states in the side menu after clicking on said button, but still this didn't work.
Can anyone tell me exactly (i.e. what code to write and where exactly it goes) how to make this happen?
The type of the sender should be UIButton, when creating the IBAction function.
The sender is your button. The function is called when you tap on the button. You must use the function setTitle on the sender(your button).
#IBAction func buttonTouchedUpInside(sender: UIButton) {
sender.setTitle("buttonName", forState: .normal)
}
Swift 4:
Plain Title
The setTitle() method will work for titles that are "Plain" as defined in the button's Attributes inspector.
#IBAction func button(sender: UIButton) {
sender.setTitle("buttonName", for: .normal)
}
Attributed Title
The setTitle() method has no effect on a button's title if it's configured as "Attributed" in the Attributes inspector. To manage this situation, first get the attributed title from the button, then set the value.
#IBAction func button(sender: UIButton) {
let attributedTitle = sender.attributedTitle(for: .normal)
attributedTitle?.setValue("buttonName", forKey: "string")
sender.setAttributedTitle(attributedTitle, for: .normal)
}
In practically, if you use button from Storyboard or Xib file, and not clear button value in (Storyboard, Xib), than setTitle method doesn't work. If you cleared button value, setTitle method is work.
Create an IBOutlet for your button, then this code should work (assuming you named your IBOutlet button):
button.setTitle("title", forState: .Normal)
button.setTitle("title 1", forState: .Application)
button.setTitle("title 2", forState: .Highlighted)
button.setTitle("title 3", forState: .Reserved)
button.setTitle("title 4", forState: .Selected)
button.setTitle("title 5", forState: .Disabled)
place it in your viewDidLoad() method.
This worked for me.
dispatch_async(dispatch_get_main_queue(), ^{
[self.btnAssment setTitle:#"ASSDate" forState:UIControlStateNormal];
});
let button = UIButton()
button.setTitle("Button", for: .normal)
button.setTitleColor(.black, for: .normal)
I just had a similar issue, and none of the answer here worked in my case.
For me the solution was to call button.layoutSubviews() to update UIButton's label just after i changed the title.
(also I had to change the button type to custom, otherwise the title change would blink)

Referencing a UIButton by tag value

I am aware that a pushed button can be updated by reference to sender.
However I want to access a button from the program body by reference to its tag and then update its label,if that is possible.
Suggestions on how to do this (in Swift) would be appreciated.
To get a button inside a view using its tag, you can use the UIView.viewWithTag function.
if let button = self.view.viewWithTag(tag) as? UIButton
{
button.setTitle("newTitle", forState: UIControlState.Normal)
}
You have to use viewWithTag
ex:
var button = self.view.viewWithTag(tagNumber) as UIButton
button.setTitle("Button Title", forState: UIControlState.Normal)
Set the tag for all buttons. Then have a property
var buttons = [UIButton]
and in your awakeFromNib init it
buttons.append[myButtonWithTag0]
buttons.append[myButtonWithTag1]
// etc
Now you can access your buttons via
let button = buttons[index]
Swift3 : Possible with viewWithTag(button_tag) as:
let tmpButton = self.view.viewWithTag(tag) as? UIButton
tmpButton?.setTitleColor(.red, for: .normal)
tmpButton?.setTitle("Button Title", for: .normal)

Hide UIButton title

I have several UIButtons in a scrollview which I use in order to pass certain information. The information is saved in the title of each uibutton and when the button is clicked, it passes its title into the function.
All I want to do is hide the title of the button so you can not see the button. I have them overlaid over images which I use to show buttons. I have the text set to transparent but it still turns white when it is being clicked.
If you include code in your explanation, please explain where it should go.
After IOS7, If you want to just hide the title on titleLabel of a button you can do as follow. This way the title is still there it just makes it invisible. if you do NSLog("%#",button.currentTitle) you will see the title in terminal. Hope this helps.
[button setTitle:#"Button Title" forState:UIControlStateNormal];
button.titleLabel.layer.opacity = 0.0f;
I found only one correct working way:
//hide
yourButton.setTitleColor(UIColor.clearColor(), forState: .Normal)
//show (put your color)
yourButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
using button.titleLabel.hidden = YES will not work (at least on on iOS 7).
I ended up using:
// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];
You can hide the label inside the button:
button.titleLabel.hidden=YES;
or set the button's title to #"" and save the value somewhere else when you want to retrieve it.
I create a subclass of UIButton and override layoutSubviews method. Hiding titleLabel in layoutSubviews method works.
public class LoadingButton: UIButton {
public var isTitleHidden: Bool = false {
didSet {
titleLabel?.isHidden = isTitleHidden
}
}
public override func layoutSubviews() {
super.layoutSubviews()
titleLabel?.isHidden = isTitleHidden
}
}
if wanna hide titleLabel, just set isTitleHidden = true
I could not remove the title from the titleLabel nor the whole view as I needed it for constraints.
I ended up using
isEnabled = false
titleLabel?.layer.opacity = 0
setTitleColor(.clear, for: .disabled)
to hide the title and
isEnabled = true
titleLabel?.layer.opacity = 1
setTitleColor(titleColor(for: .normal), for: .disabled)
to show it again
To hide a title temporary just setTitle to empty string
setTitle("", for: .normal)
the button title label will be hidden but the title will still in the titleLabel, you can return it back using
setTitle(titleLabel?.text, for: .normal)
If you want to temporary hide the title, while you disabling the button, use:
setTitle("Title", for: .normal)
setTitle("", for: .disabled)
Then, button.isEnabled = false, when you want to hide the title.
I got a problem with title, because used an attributed title and nothing above helped. Then i found a workaround:
button.titleEdgeInsets = .init(top: 0, left: shouldHide ? 1000 : 0, bottom: 0, right: 0)
However it has some disadvantages, but fit my needs.
I've come up with this solution, which allows you to set title label text and use it with button image without showing it and not moving button image to the left.
- (void)hideButtonLabel:(UIButton*)buttonInp {
buttonInp.titleLabel.layer.opacity = 0.0f;
uttonInp.titleLabel.font = [UIFont fontWithName:#"Helvetica-Light" size:0.0];
}
You cannot hide UIButton titleLabel using .hidden property. Instead you can do this.
To Hide:
[self.yourButton setTitle:nil forState:UIControlStateNormal];
To Show:
[self.yourButton setTitle:#"Your Text" forState:UIControlStateNormal];
In Swift-
You don't need to hide nor need to make the opacity to 0.0. Swift gave you a simpler way.
Just set the title as nil. In fact, I got the idea from the documentation.
Command click on the setTitle(_:for:) method and you will see-
open func setTitle(_ title: String?, for state: UIControl.State) // default is nil. title is assumed to be single line
So, I just set it to nil.
setTitle(nil, for: .normal)
Swift 5 to hide button label:
myButton.titleLabel?.isHidden = true
Here myButton is a #IBOutlet of the button.