One UIbutton can be chosen at a time - ios

I have three buttons,and only one button can be chosen by the user at a time,that means that other then the initial state where none are chosen only one button can be at a highlighted state at a time.After another button (a forth one) is clicked,all the previous button's abilities to be clicked on are disabled.How do I do this?

button.enabled = false /// this will make a button disabled in swift

Why not use the 4th button to toggle a bool - that can be used to check on the state of the other buttons?
var lockedButtons: Bool = true
#IBAction func toggleButtonLock(sender: UIButton) {
lockedButtons = !lockedButtons
}
#IBAction func Button1(sender: UIButton) {
if !lockedButtons{
//Button1 code
}
}
//button 2 & 3 code similar....

Related

append button title to array?

I have an empty array, and I have #IBAction func test(_ sender: UIButton) with 25 different buttons attached to this func. how can I write the code for appending a button title to the array, when pressed?
someArray.append = (this part that I can't figure out?)
Since I don't know which button/buttons will be pressed I can't just write someArray.append = ("buttonTitle")
(ive googled and searched in here for hours, but I can't find the solution)
if let title = sender.title(for: sender.state) {
someArray.append(title)
}
For multiple UIButtons with single IBOutlet Action you can add tags for each UIButton.
So if you've used StoryBoard for UIButtons, so when you click on single UIButton - navigate to Attribute Inspector & add tag to each button. As shown in below fig.1.0
I've set all UIButtons with different title & tags.
Once you set tags, for all buttons, then programmatically you can identify which button is clicked via single - onClick function.
#IBAction func Click(_ sender: UIButton) {
if let title = sender.title(for: sender.state){
print(title)
//Here we are identifying which button is pressed. If I pressed UIButton.tag = 1 then I'm just printing it's title, else appending UIButton value in array.
if sender.tag == 1 {
print(sender.title(for: sender.state))
}
else{
self.someArray.append(title)
}
}
}
So, in this way you can identify all your UIButton actions within a single event action.
#IBOutlet weak var btnoutlet: UIButton!
if let title = btnoutlet.currentTitle {
someArray.append(title)
}

How to toggle background between two buttons in Swift?

I created an app with two buttons. What I want is when the left button is clicked, it will change it's background color. If the right button is clicked, change that background color.
Above concept is working with the following code for both buttons:
#IBAction func pressedButton(_ sender: UIButton) {
sender.backgroundColor = (sender.backgroundColor == Constants.BOOL_BUTTON.enabled) ? Constants.BOOL_BUTTON.disabled : Constants.BOOL_BUTTON.enabled
}
Both buttons are linked to this function. The only thing what is not working, is that when the left button for example had the enabled color and the user then clicks on the right button to change his choice, the left button will not be resetting to it's disabled color. How can I achieve this with above function?
The problem with your code is when right or left button is pressed, it doesn't know about the state of the other one. What you can do is:
#IBAction func pressedButton(_ sender: UIButton) {
if sender.backgroundColor == Constants.BOOL_BUTTON.enabled {
return
}
sender.backgroundColor = Constants.BOOL_BUTTON.enabled
switch sender {
case self.leftButton:
self.rightButton.backgroundColor = Constants.BOOL_BUTTON.disabled
case self.rightButton:
self.leftButton.backgroundColor = Constants.BOOL_BUTTON.disabled
default:
return
}
}

Customize a button and its highlighted state in Swift3 using a class

I'm new to Swift and I assume this is a fundamental question to programming for iOS.
I have three buttons in my storyboard and I want to customize how those buttons look if pressed once, twice and three times.
I also have three themes (pink, blue and orange). What I thought of doing is to create three new classes called pink,blue and orange.swift
I don't want to create them programmatically, only style them programmatically.
What I lack to understand is how do I call the function (Example: "ButtonIsPressed") from my pink.swift class into my #IBAction and #IBOutlet in the main view controller that is also object oriented (ie. I don't want to create a function for every button)?
I can't really find a decent and up-to-date Swift 3 Tutorial for this, any help or advice on this topic will be greatly appreciated.
Why can it not be as simple as?:
#IBAction func buttonPressed(_ sender: UIButton!) {
self.backgroundColor = myPinkCGolor
}
I think shallowThought's answer will work for changing backgroundColor based on button state of a specifically named IBOutlet.
I have three buttons in my storyboard and I want to customize how those buttons look if pressed once, twice and three times.
If you want to maintain "state", as in have a "counter" for how many times a button's been clicked or tapped, you can use the "tag" property of the button. Set it to zero, and in your IBAction functions increment it. (Like shallowThought said, use .touchUpInside and .touchDown for the events.)
Also, you have one minor - but important! - thing wrong in your code Brewski:
#IBAction func buttonPressed(_ sender: UIButton!) {
self.backgroundColor = myPinkCGolor
}
Should be:
#IBAction func buttonPressed(_ sender: UIButton!) {
sender.backgroundColor = myPinkCGolor
}
So combining everything - up vote to shallowThought (also, changing his AnyObject to UIButton and making it Swift 3.x syntax on the UIColors - and would end up with this. Note that there is no need for an IBOutlet, and you can wire everything up in IB without subclassing:
// .touchUpInside event
// can be adapted to show different color if you want, but is coded to always show white color
#IBAction func buttonClicked(sender: UIButton) {
sender.backgroundColor = UIColor.whiteColor()
}
// .touchDown event
// will show a different color based on tap counter
#IBAction func buttonReleased(sender: UIButton) {
switch sender.tag {
case 1:
sender.backgroundColor = UIColor.blue
case 2:
sender.backgroundColor = UIColor.red
case 3:
sender.backgroundColor = UIColor.green
default:
sender.backgroundColor = UIColor.yellow
}
sender.tag += 1
}
There is no methode to set the backgroundColor for a certain state, like there is for other UIButton properties, so you have to listen to the buttons actions:
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
#IBAction func buttonClicked(sender: AnyObject) { //Touch Up Inside action
button.backgroundColor = UIColor.whiteColor()
}
#IBAction func buttonReleased(sender: AnyObject) { //Touch Down action
button.backgroundColor = UIColor.blueColor()
}
...
}
or set a unicolor image withimage:UIImage, forState:.selected.

Can we make 1 UIButton for 2 action with swift?

i want to make 2 action for a button like that.
selected and deselected action for 1 button.
#IBAction func btntouch(sender: UIButton) {
if firsttouch
{
print bla bla
change button to selected style. maybe background color.
}
else
{
}
}
how can i do that?
In case you need to split two button statuses - like ON and OFF, try this:
var buttonSwitched : Bool = false
#IBAction func btntouch(sender: UIButton) {
//this line toggle your button status variable
//if true, it goes to false, and vice versa
self.buttonSwitched = !self.buttonSwitched
if self.buttonSwitched
{
//your UI styling
}
else
{
//your opposite UI styling
}
}
Create 2 IBActions:
#IBAction func touchDown(_ sender: AnyObject) {
print("down")
}
#IBAction func touchUp(_ sender: AnyObject) {
print("up")
}
When connecting the first one, make sure the event is set to touchDown. For the second one, make sure it is set to touchUpInside
Yes, you can. Depending on your requirements, you could store the current state of the button in the view controller or in the model.
If the visual change caused by the first touch needs to be persisted across opening and closing of your view controller, store the value indicating the change in the model; if you need to reset the visuals when the view controller shows, store the value in the view controller itself:
var firstTouch = true
#IBAction func btntouch(sender: UIButton) {
if firstTouch {
firstTouch = false
...
} else {
...
}
}

Simple way to set Button Active Color in iOS Swift

Ive been looking around for methods to change active state on Button click. I have 4 buttons when clicked the reposition my scroll view as programmed.
I am trying to set the background color to fade a bit if clicked. Im able to set the background color but it stays the same faded color when another is clicked. It doesnt return to an inactive state.
Any simple way to acheive this onckick button behavior globally?
heres my button click func's:
#IBAction func tab1(sender: UIButton)
{
slScrollView.setContentOffset(CGPointMake(0.0, 0.0), animated: true)
tab1.backgroundColor = UIColor.grayColor()
tab2.selected = false
tab3.selected = false
}
#IBAction func tab2(sender: UIButton)
{
slScrollView.setContentOffset(CGPointMake(0.0, 650.0), animated: true)
tab2.backgroundColor = UIColor.grayColor()
tab1.selected = false
tab3.selected = false
}
#IBAction func tab3(sender: UIButton) {
slScrollView.setContentOffset(CGPointMake(0.0, 1370.0), animated: true)
tab3.backgroundColor = UIColor.grayColor()
tab1.selected = false
tab2.selected = false
}
First, create an IBOutletCollection (or four separate outlets) to the buttons. Then create an IBAction method and set all four buttons to fire it when tapped. In the method, do the background fade animation on the button that fired the action (which is passed into the handler as its sender argument, then reset the states of the other outlet buttons.
The way I would code it:
// Outlet to all of the buttons. ctrl+drag each button to this outlet.
#IBOutletCollection buttons = [UIButton]()
// Set *all* of the buttons to fire this method.
#IBAction func buttonTapped(sender: AnyObject!) {
(sender as? UIButton).backgroundColor = <whatever>
for button in buttons.filter({ $0 != sender }) {
button.backgroundColor = <default>
}
}

Resources