How to transfer information from one IBAction to another - ios

The code should work as follows: On one View I click tag button and tagButtonPressed IBAction should set tag variable to sender.tag of the tag button. It works well. Then user should click 'send' button and sharePressed IBAction should use the tag variable which equals to sender.tag of the 'tag' button, and print the updated value of tag variable. Unfortunately it doesn't work.
import UIKit
class ShareViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var tag : Int = 1
#IBOutlet weak var shareTextField: UITextView!
#IBAction func tagButtonPressed(_ sender: UIButton) {
tag = sender.tag // lets say sender.tag = 4
print(tag) // It prints 4
}
#IBAction func sharePressed(_ sender: Any) {
print(tag) // it prints 1 and i want it to print also 4
}
}

Your code is correct. Somewhere else (not shown) you're re-setting the tag value to 1.

Related

My Question is about changing a UIImage when a button is pressed

So I am quite new to coding, learning Swift 3 on Udemy. I'm trying to test my skills by building a music app that contains 3 sound files, at the moment I am struggling to get the image of the current song that should be playing once my UIButton is pressed. I have created an array containing the image files but for some reason it only shows 2 out of the 3 images and will not go further nor will it let me loop the images, any and all suggestions are welcome.
I have tried a for-in loop which is not what I want at the moment. I am trying to get the function to update songImage to accept my array and link to the sender.tag property to cycle through the images
class ViewController: UIViewController {
// Instance Variables
var playTheSong : AVAudioPlayer!
var imageArray = ["songImage1", "songImage2", "songImage3"]
var allSongNamesAndDescriptions = MusicClassBank()
var nextImage = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
albumArtwork.image = UIImage(named: "songImage3")
}
#IBAction func buttonPressed(_ sender: UIButton) {
updateSongImage(selectedImageFile: imageArray[sender.tag - 1])
nextImage = nextImage + 1
}
#IBOutlet weak var albumArtwork: UIImageView!
#IBOutlet weak var nameOfSong: UILabel!
#IBOutlet weak var songDescription: UILabel!
// Cycle through images upon button being pressed.
func updateSongImage(selectedImageFile : String) {
if nextImage <= 3 {
albumArtwork.image = UIImage(named: selectedImageFile)
}
else {
nextImage = 0
}
}
Right now the code is showing just the image displayed upon view load and the next image in the array. I cannot get it to go through the entire array and keep going when the button is pressed.
You are using the "next" UIButton sender.tag as a parameter to change the image, but that tag never changes. So:
In your viewDidLoad() method, you show the third image
When you press the "next" button, you update the image to 1 (probably the sender.tag is 1)
Pressing the button again don't update the image
You can try something like this:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Set the first image
updateSongImage(selectedImageFile : imageArray[0])
}
#IBAction func buttonPressed(_ sender: UIButton) {
//If nextImage is less than imageArray.count - 1 (because arrays start with 0), add one to nextImage. Else, nextImage return to zero
nextImage = nextImage < imageArray.count - 1 ? nextImage + 1 : 0
updateSongImage(selectedImageFile: imageArray[nextImage])
}
func updateSongImage(selectedImageFile : String) {
//Here we only need to update the image, because the if is outside
albumArtwork.image = UIImage(named: selectedImageFile)
}

How can I change the title of a group of buttons with an array?

I have two IBAction groups with three buttons each.
Each button has a different tag - within each group: 1, 2 and 3.
I have three arrays, with three values each.
I want to press the first button of the first IBAction and change the title of the three buttons in the second IBAction, based on the group of arrays.
Here is the code:
import UIKit
class ViewController: UIViewController {
var firstArray = ["A1","A2","A3"]
var secondArray = ["B1","B2","B3"]
var thirdArray = ["C1","C2","C3"]
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func greenButtons(_ sender: UIButton) {
if sender.tag == 1 {
//change the title of the grayButtons with the firstArray
}
if sender.tag == 2 {
//change the title of the grayButtons with the secondArray
}
if sender.tag == 3 {
//change the title of the grayButtons with the thirdArray
}
}
#IBAction func grayButtons(_ sender: UIButton) {
}
}
You can try
// create outlet group for each
#IBOutlet weak var greenButtons:[UIButton]!
#IBOutlet weak var grayButtons:[UIButton]!
Inside action of greenButtons
grayButtons.forEach { $0.setTitle(firstArray[$0.tag - 1],for:.normal) }
Instead of if check you can construct the array as
let allArr = [ ["A1","A2","A3"], ["B1","B2","B3"],["C1","C2","C3"]]
Then
grayButtons.forEach { $0.setTitle(allArr[sender.tag - 1][$0.tag - 1],for:.normal) }

Use Segmented Control Swift 3

I have searched the web and youtube about segmented control, but I only find examples of how to change to a different view.
My goal is to take the selected value from the segmented control and use it in the action on another button.
I will try to explain what I mean :
#IBAction func segmentetControll(_ sender: UISegmentedControl) {
value 1
value 2
value 3
}
#IBAction func calculateButton(_ sender: Any) {
if value 1 {
do this
}
else if value 2 {
Do that
}
else if value 3 {
Do thids
}
You need to connect it via #IBOutlet to file.
Then you can use its index to do whatever you want.
#IBOutlet var segmentedControl: UISegmentedControl!
#IBAction func calculateButton(_ sender: Any) {
let index = segmentedControl.selectedSegmentIndex
//...
}
Putting the code in the IBAction method connected via the storyboard or Xib file is the correct approach. You can use strong types when hooking up the action to the control and write code such as:
#IBAction func calculateButton(_ sender: UISegmentedControl) {
switch(sender.selectedSegmentIndex){
case 1:
// Do something
case 2:
// Do something
case 3:
// Do something
default:
break
}
}
You would need to create an #IBOutlet var segmentedControl: UISegmentedControl? property that connects to the segmented control in the Interface Builder, and then in the calculateButton method you can switch over segmentedControl.selectedSegmentIndex. Let me know if you need further explanation.

How to use textfield to enter numbers into a array (swift3)

I have a textfield that I would like the user to be able to enter numbers for a array in. Ideally the button would save the number, then clear the textfield for another number to be enter. This would be a process that would just go in a loop. So its just save the number, clear the number, repeat infinitely.
import UIKit
class ViewController: UIViewController {
#IBOutlet var enterText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func submitText(_ sender: Any) {
}}
Lets define and array.
var arrayOfInt = [Int]()
Suppose you have button action method and textField Object (could be IB outlet) to which user enters number.
#IBAction func submitText(_ sender: Any) {
if let text = textField.text {
if let number = Int(text){
arrayOfInt.append(number)
}else {
print("Please enter number")
}
}
}

my sender.tag switch statement does not work

My switch doesn't work, when i press the first button which is "buttonAclicked" a label is showing which is also what i wanted but when i try to click on the button with the tag 3 i get a SIGABRT error which crashes the whole thing.
I know that the button with the tag 3 works since i have made an IBAction for itself where it printed out:
">"
but when i set it into the switch statement it does not work, nothing is being printed besides errors and the other prints but not the last print
func buttonAclicked(sender: UIButton) {
print("button A was pressed")
label.hidden = false
label.setNeedsDisplay()
if(sender.isKindOfClass(UIButton)) {
print("hey")
}
switch sender.tag {
case 3 :
print("buuuutttttooonnn")
break
default :
label.text = "0"
}
}
create proper Button IBAction and connect to storyboard proper. it should solve your SIGABRT error.
your error because of not connect IBAction Properly to Storyboard
#IBOutlet weak var buttonOutlat: UIButton! // creat Button Outlet hera
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
buttonOutlat.tag = 3 // <- you can set sender.tag here
}
#IBAction func buttonAclicked(_ sender: UIButton) { // creat proper Button Action
print("button A was pressed")
switch sender.tag{
case 3 :
print("buuuutttttooonnn---3")
label.text = "button-3"
break
default :
label.text = "button-default"
}
}

Resources