UIButton doing different tasks - swift

How do i make a UIButton do something different every time it is pressed in swift. Ive tried making two buttons that overlap and one hides when it is clicked Didn't work though.
EDIT: nevermind i found i could do this for a mute/play button
If backgroundaudio.play
Backgroundaudio.pause
Else
Backgroundaudio.play

You can do it in two different ways:
Changing the target of the button:
Adding a target:
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
Removing a target:
button.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
Or have a logical statement inside your action function:
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonAction(sender: UIButton!)
{
//your conditions go here
}

For example: To do something every other time you could write:
func oneThing() {
// Do your stuff
}
func otherThing() {
// Do other stuff
}
var shouldDoOtherThing = false
#IBAction func clicked(sender: UIButton) {
if shouldDoOtherThing {
otherThing()
} else {
oneThing()
}
shouldDoOtherThing = !shouldDoOtherThing
}
You could also just do the stuff you want to do inside the if statement like
var shouldDoOtherThing = false
#IBAction func clicked(sender: UIButton) {
if shouldDoOtherThing {
// Do other stuff
} else {
// Do your stuff
}
shouldDoOtherThing = !shouldDoOtherThing
}
Or you can use anything else that changes what code should execute (they are called Control Statements)

I just tested this out. You'll only need one button.You can do as many random things as you want
#IBAction func button_click(sender: AnyObject) {
var randomNumber = Int(arc4random_uniform(5) + 1) // generates random number 1 - 5
println(randomNumber)
switch randomNumber { // perform method depending on what random number was generated
case 1 :
funcOne() // Run this method
println("Pressed 1")
case 2:
funcTwo() // Runs this method
println("Pressed 2")
case 3:
funcThree() // Runs this method
println("Pressed 3")
case 4:
funcFour() // Runs this method
println("Pressed 4")
case 5:
funcFive() // Runs this method
println("Pressed 5")
default:
println("do nothing")
}
}

Related

Swift - How to get the sender tag for an array of buttons using UILongPressGestureRecognizer?

I have buttons in the storyboard that I put into a Referencing Outlet Collection. I'm using UITapGestureRecognizer and UILongPressGestureRecognizer for all of these buttons, but how can I print exactly which button gets tapped? Bellow is what I tried but doesn't work. I get an error that says "Value of type 'UILongPressGestureRecognizer' has no member 'tag'." I'm trying to build the button grid for the Minesweeper game. Thank you for your help.
class ViewController: UIViewController {
#IBOutlet var testButtons: [UIButton]! // There are 100 buttons in this array
override func viewDidLoad() {
super.viewDidLoad()
let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
testButtonPressed.minimumPressDuration = 0.5
// These indexes are just to test how to recognize which button gets pressed
testButtons[0].addGestureRecognizer(testButtonPressed)
testButtons[1].addGestureRecognizer(testButtonPressed)
}
#objc func testPressed(_ sender: UILongPressGestureRecognizer) {
print("Test button was pressed")
print(sender.tag) // THIS DOESN'T WORK, BUT CONCEPTUALLY THIS IS WHAT I WANT TO DO
}
This error occurs because UILongPressGestureRecognizer object has no tag property
You can access sender's button in a way like that:
#objc func testPressed(_ sender: UILongPressGestureRecognizer) {
guard let button = sender.view as? UIButton else { return }
print(button.tag)
}
I think that the best solution to handle button's actions is to add #IBAction
(you can add it like #IBOutlet with a minor change - set Action connection type)
And then in #IBAction block you cann access all button properties (like tag and others)
instead of using gesture I think it would be better to use #IBAction and connect the buttons With it here is a small example
UILongPressGestureRecognizer which is a subclass of UIGestureRecognizer, can be used only once per button or view. Because UILongPressGestureRecognizer has only a single view property. In your code, it will always be testButtons[1] calling the testPressed action. So you have to first modify the viewDidLoad code like this :-
for button in testButtons {
let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
testButtonPressed.minimumPressDuration = 0.5
button.addGestureRecognizer(testButtonPressed)
button.addGestureRecognizer(testButtonPressed)
}
Then you can access the button from testPressed like this (I hope you've already set the tag in the storyboard) :-
#objc func testPressed(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
if let button = sender.view as? UIButton {
print(button.tag)
}
}
}
You need to set tags before pressing!
On the viewDidLoad() method you must add something like:
testButtons.enumerated().forEach {
let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
testButtonPressed.minimumPressDuration = 0.5
$0.element.addGestureRecognizer(testButtonPressed)
$0.element.tag = $0.offset
}
And when the long press is receiving you need to get a tag from view not from the sender!
print(sender.view?.tag)
Since a gesture recognizer should only be associated with a single view, and doesn't directly support using an identity tag to match it with buttons. When creating an array of buttons for a keyboard, with a single gesture response function, I found it easier to use the gesture recognizer "name" property to identify the associated button.
var allNames: [String] = []
// MARK: Long Press Gesture
func addButtonGestureRecognizer(button: UIButton, name: String) {
let longPrssRcngr = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressOfButton(gestureRecognizer:)))
longPrssRcngr.minimumPressDuration = 0.5
longPrssRcngr.numberOfTouchesRequired = 1
longPrssRcngr.allowableMovement = 10.0
longPrssRcngr.name = name
allNames.append(name)
button.addGestureRecognizer(longPrssRcngr)
}
// MARK: Long Key Press
#objc func longPressOfButton(gestureRecognizer: UILongPressGestureRecognizer) {
print("\nLong Press Button => \(String(describing: gestureRecognizer.name)) : State = \(gestureRecognizer.state)\n")
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
if let keyName = gestureRecognizer.name {
if allNames.contains(keyName) {
insertKeyText(key: keyName)
} else {
print("No action available for key")
}
}
}
}
To implement, call the addButtonGestureRecognizer function after creating the button, and provide a name for the button (I used the button text) e.g.
addButtonGestureRecognizer(button: keyButton, name: buttonText)
The button name is stored in the "allNames" string array so that it can be matched later in "longPressOfButton".
When the button name is matched in the "longPressOfButton" response function, it passes it to "addKeyFunction" where it is processed.

Swift 4 - Creating a common function for multiple buttons

I'm wondering if there is a more efficient way to code an action that is the same with the exception of which button has been pressed and which item in a struct it relates to. Basically, I have a struct of 10 variables all of which are a boolean type and I have 10 buttons. When the user presses the button, I want to check whether it has already been pressed (using the struct) and then change the background of the button depending on the state and reverse the state. I've copied my current code for one of the buttons but thought I should be able to avoid doing this 10 times!
#IBAction func architectureButtonPressed(_ sender: Any) {
if myInterests.architecture {
myInterests.architecture = false
architectureButton.setBackgroundImage(imageUncheckedNarrow, for: .normal)
} else {
myInterests.architecture = true
architectureButton.setBackgroundImage(imageCheckedNarrow, for: .normal)
}
}
Well one simple way is to have each UIButton point to the same architectureButtonPressed IBAction method. Since the button that's pressed is passed into the method (sender) you can consult it's tag property to know the index of which field in your struct should be updated. (And then you might want to change your struct to just store an array of 10 bools, but up to you).
Then for each UIButton, whether programmatically in storyboard or nib, you'd assign the appropriate index value to the button's tag field.
Create yours IBOutlet for each button.
Create a array and store all buttons like : var arrayButtons : [UIButton] = []
arrayButtons.append[button1]
arrayButtons.append[button2]
...
Create a array of booleans to store true/false: var arrayBools : [Bool] = [] and initialize if some value.
Note that the indexes of the arrayButtons and arrayBools must be same related.
Create selector function to listen touch buttons.
button.addTarget(self, action: #selector(my_func), for: .touchUpInside)
#objc func my_func(_ sender : UIButton) {
for i in 0...arrayButtons.size-1 {
if arrayButtons[i] == sender {
if arrayBooleans[i] {
arrayBooleans[i] = false
arrayButtons[i].setImage()
} else {
arrayBooleans[i] = true
arrayButtons[i].setImage()
}
}
}
}
My suggestion is to manage the images in Interface Builder via State Config (Default/Selected)
Then assign an unique tag starting from 100 to each button and set the isSelected value in the IBAction to the corresponding struct member in a switch statement:
#IBAction func buttonPressed(_ sender: UIButton) {
switch sender.tag {
case 100: myInterests.architecture = sender.isSelected
case 101: myInterests.art = sender.isSelected
...
default: break
}
}
Alternatively use Swift's native KVC with WriteableKeypath
let keypaths : [WritableKeyPath<Interests,Bool>] = [\.architecture, \.art, \.fashion, \.history, \.localCulture, \.music, \.nature, \.shopping, \.sport, \.anything]
#IBAction func buttonPressed(_ sender: UIButton) {
let index = sender.tag - 100
let keypath = keypaths[index]
myInterests[keyPath: keypath] = sender.isSelected
}

Swift Xcode attach an action to a checkbox using a checkbox subclass

I’m new to swift so I am stumped on something that is probably very simple.
I want to have checkboxes for each day of the week and be able to toggle their states a bit like the standard Apple clock app alarm repeat page. I am using a subclass borrowed from https://github.com/kenthinson/checkbox/ to create a set of checkboxes. All that works like a charm in the storyboard but now I am struggling with how to refer to the subclass checkbox ‘state’ in my view controller to actually do something.
So here is the subclass:
import UIKit
class checkBox: UIButton {
//images
let checkedImage = UIImage(named: "checkBoxChecked")
let unCheckedImage = UIImage(named: "checkBoxUnchecked")
//bool property
var isChecked:Bool = false{
didSet{
if isChecked == true{
self.setImage(checkedImage, forState: .Normal)
}else{
self.setImage(unCheckedImage, forState: .Normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
self.isChecked = false
}
func buttonClicked(sender:UIButton) {
if(sender == self){
if isChecked == true{
isChecked = false
}else{
isChecked = true
}
}
}
}
Here is an outlet example:
#IBOutlet weak var sun: checkBox!
Here is my empty array ready to be appended (working):
var daysArray:[String] = []
Here is an example action:
#IBAction func setSunday(sender: checkBox) {
if (****what do I do here to retrieve the button state?****) {
daysArray.append("0")
print(daysArray) // Check the array is working then delete
} else {
daysArray.removeAtIndex(0)
print(daysArray) // Check the array is working then delete
}
}
If I can get this working then I can apply to all days of the week and make the state of the array persistent using NSUserDefaults.
Hope you guys can help.
Use sender.isChecked to retrieve the state:
#IBAction func setSunday(sender: checkBox) {
if sender.isChecked {
daysArray.append("0")
print(daysArray) // Check the array is working then delete
} else {
daysArray.removeAtIndex(0)
print(daysArray) // Check the array is working then delete
}
}

Swift: How to pass a Parameter to a function which itself is also a Parameter?

It's like this:
var i = 0
button.addTarget(self, action:"showContent", forControlEvents: UIControlEvents.TouchUpInside)
and the function "showContent" is like this:
func showContent(i: Int) {
//do sth.
}
I want to pass the variable i to function showContent when that button be touched, how could i do ?
In the target-action pattern, you can't do that. You can either use "showContent", in which case your function will be:
func showContent() {
}
… or you can add a colon ("showContent:") in which case your function will be:
func showContent(sender : UIButton) {
// do something with sender
}
This helps enforce the broader Model / View / Controller pattern by making it difficult for your views to be "smart". Instead, your model and controller objects should handle i. A button simply displays what it's told, and tells your controller when it's tapped.
You can read i, and respond to it, in the appropriate method. For example:
class ViewController : UIViewController {
var i = 0
var button = UIButton(type: .System)
var label = UILabel()
override func viewDidLoad() {
button.addTarget(self, action:"showContent", forControlEvents: UIControlEvents.TouchUpInside)
}
func showContent() {
i++
label.text = "Current value of i: \(i)"
}
}

Get button pressed id on Swift via sender

So I have a storyboard with 3 buttons I want to just create 1 action for all those 3 buttons and decide what to do based on their label/id...
Is there a way to get some kind of identifier for each button?
By the way they are images, so they don't have a title.
#IBAction func mainButton(sender: UIButton) {
println(sender)
}
You can set a tag in the storyboard for each of the buttons. Then you can identify them this way:
#IBAction func mainButton(sender: UIButton) {
println(sender.tag)
}
EDIT: For more readability you can define an enum with values that correspond to the selected tag. So if you set tags like 0, 1, 2 for your buttons, above your class declaration you can do something like this:
enum SelectedButtonTag: Int {
case First
case Second
case Third
}
And then instead of handling hardcoded values you will have:
#IBAction func mainButton(sender: UIButton) {
switch sender.tag {
case SelectedButtonTag.First.rawValue:
println("do something when first button is tapped")
case SelectedButtonTag.Second.rawValue:
println("do something when second button is tapped")
case SelectedButtonTag.Third.rawValue:
println("do something when third button is tapped")
default:
println("default")
}
}
If you want to create 3 buttons with single method then you can do this by following code...Try this
Swift 3
Example :-
override func viewDidLoad()
{
super.viewDidLoad()
Button1.tag=1
Button1.addTarget(self,action:#selector(buttonClicked),
for:.touchUpInside)
Button2.tag=2
Button2.addTarget(self,action:#selector(buttonClicked),
for:.touchUpInside)
Button3.tag=3
Button3.addTarget(self,action:#selector(buttonClicked),
for:.touchUpInside)
}
func buttonClicked(sender:UIButton)
{
switch sender.tag
{
case 1: print("1") //when Button1 is clicked...
break
case 2: print("2") //when Button2 is clicked...
break
case 3: print("3") //when Button3 is clicked...
break
default: print("Other...")
}
}
You can create an outlet for your buttons and then implement:
#IBAction func mainButton(sender: UIButton) {
switch sender {
case yourbuttonname:
// do something
case anotherbuttonname:
// do something else
default: println(sender)
}
}
Swift 4 - 5.1
#IBAction func buttonPressed(_ sender: UIButton) {
if sender.tag == 1 {
print("Button 1 is pressed")
}
}
You have to set tag value to what you need and access it with
sender.tag
Assuming you gave them all proper names as #IBOutlets:
#IBOutlet var weak buttonOne: UIButton!
#IBOutlet var weak buttonTwo: UIButton!
#IBOutlet var weak buttonThree: UIButton!
You can use the following to determine which is which
#IBAction func didPressButton(sender: AnyObject){
// no harm in doing some sort of checking on the sender
if(sender.isKindOfClass(UIButton)){
switch(sender){
case buttonOne:
//buttonOne action
break
case buttonTwo:
//buttonTwo action
break
case buttonThree:
//buttonThree action
break
default:
break
}
}
Swift 3 Code:
In xcode Please set tag for each button first to work following code.
#IBAction func threeButtonsAction(_ sender: UIButton) {
switch sender.tag {
case 1:
print("do something when first button is tapped")
break
case 2:
print("do something when second button is tapped")
break
case 3:
print("do something when third button is tapped")
break
default:
break
}
}
You can do like this, just you have to give tag to all the buttons and do like this:
#IBAction func mainButton(sender: AnyObject)
{
switch sender.tag {
case 1:
println("do something when first button is tapped")
case 2:
println("do something when second button is tapped")
case 3:
println("do something when third button is tapped")
default:
println("default")
}
}
Use the outlets instead, tags clutter the code and make the readability way worse. Think about the poor developer that reads the code next and sees if sender.tag = 381 { // do some magic }, it just won't make any sense.
My example:
class PhoneNumberCell: UITableViewCell {
#IBOutlet weak var callButton: UIButton!
#IBOutlet weak var messageButton: UIButton!
#IBAction func didSelectAction(_ sender: UIButton) {
if sender == callButton {
debugPrint("Call person")
} else if sender == messageButton {
debugPrint("Message person")
}
}
[...]
}
You could also do this in a nice switch as well, which would make it even better.
Tested on Swift 5.1
Swift 4
add tag on button
let button = UIButton()
button.tag = 10
click event
#IBAction func mainButton(sender: UIButton) {
switch sender.tag {
case 10:
print("10")
case 11:
print("11")
default:
print("yes")
}
}
In this case you can use NSObject extension Accessibility Element UIAccessibility.
I have used accessibilityLabel and accessibilityIdentifier both are success in call and condition checking.
First
You can set a Accessibility Label or Identifier in the storyboard for each of the buttons in Identity inspector. Accessibility should be enabled.
To check/Identify button by
#IBAction func selectionPicker(_ sender: UIButton){
if sender.accessibilityLabel == "childType"{ //Check by accessibilityLabel
print("Child Type")
}
if sender.accessibilityIdentifier == "roomType"{ //Check by accessibilityIdentifier
print("Room Type")
}
performSegue(withIdentifier: "selectionViewSegue", sender:sender)
}
On Swift 3.2 and 4.0 with Xcode 9.0
Given the case you labeled your buttons "1", "2", "3":
#IBAction func mainButton(sender: UIButton) {
switch sender.titleLabel?.text {
case "1":
print("do something when first button is tapped")
case "2":
print("do something when second button is tapped")
case "3":
print("do something when third button is tapped")
default:
() // empty statement or "do nothing"
}
}
Swift 5.5
I have utility methods in other classes that do not have an instance of my ViewController, so I don't compare the sent objects to what is defined in the ViewController's IBOutlets.
I don't use tags if I can use a plain language identifier on my UI objects. I'd rather have plain language identifiers than numbers to identify my objects because it is easier for me. Just another way of doing it.
If I need to use a utility method, I set it up with a sender parameter so I can send the button and then figure out which button was clicked based on the assigned identity of the button within Storyboard.
For example:
class Utility {
func doSomething(sender: Any?) {
guard let button = sender as? NSButton else {
print("Unable to set button from sender.")
return
}
guard case buttonID = button.identifier?.rawValue else {
print("Unable to get button identifier.")
return
}
switch buttonID {
case: "firstButton":
_ = buttonID // Perform firstButton action
case: "secondButton":
_ = buttonID // Perform secondButton action
case: "thirdButton":
_ = buttonID // Perform thirdButton action
default:
// shouldn't get here - error?
}
}
}
In my ViewController I have the following buttons set up as IBOutlets and their identity is the same in Storyboard.
#IBOutlet weak var firstButton: NSButton?
#IBOutlet weak var secondButton: NSButton?
#IBOutlet weak var thirdButton: NSButton?
Then I have my IBActions:
#IBAction func firstButtonClicked(sender: Any?) {
utility.doSomething(sender: sender)
}
#IBAction func secondButtonClicked(sender: Any?) {
utility.doSomething(sender: sender)
}
#IBAction func thirdButtonClicked(sender: Any?) {
utility.doSomething(sender: sender)
}
In my case what i did, just like the answers above i used the tag to identify the specific button, what i added is that i added a UIButton extension that adds an id so that i can set a string id
i had three buttons with tags 0, 1 and 2
Then created the extension
extension UIButton {
var id: String {
let tag = self.tag
switch tag {
case 0:
return "breakfast"
case 1:
return "lunch"
case 2:
return "dinner"
default:
return "None"
}
}
}
When accessing a button in an IBAction i would just call:
sender.id
Select your first button and give it tag 0, and select second button and give it tag 1 and so on, in action check the tag bit and perform you functionalities on the basis of tag bit.:
switch sender as! NSObject {
case self.buttoneOne:
println("do something when first button is tapped")
case self.buttoneTwo:
println("do something when second button is tapped")
default:
println("default")
}