Making a button save to a variable - swift

class ViewController: UITableViewController {
#IBOutlet weak var Name: UITextField!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var Phonenumb: UITextField!
#IBOutlet weak var phoneLabel: UILabel!
#IBOutlet weak var Phone: NSLayoutConstraint!
#IBOutlet weak var Date: UITextField!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var County: UITextField!
#IBOutlet weak var killCountLabel: UILabel!
#IBOutlet weak var killDate: UITextField!
#IBOutlet weak var killDateLabel: UILabel!
#IBOutlet weak var points: UITextField!
#IBOutlet weak var pointsLabel: UILabel!
#IBOutlet weak var Email: UITextField!
#IBOutlet weak var emailLabel: UILabel!
#IBOutlet weak var Address: UITextField!
#IBOutlet weak var addressLabel: UILabel!
#IBOutlet weak var page2Button: UIButton!
#IBAction func saveTextToVar(sender: UIButton) {
var nameText: Name.text // problem is here (Error: Use of undeclared type 'Name')
}
My app is a simple form app that will send me people's forms when they fill them out. What I would like it to do is when I push the button (page2Button) is for it to send me to the second tableViewController and save all of my textFields to a variable then send them to my Firebase. (I have not installed Firebase yet).
My question is why is there an undeclared type Error when I already have a var named Name? And how do I fix it?

Related

Variable UILabel name

Is it possible to make a label as a variable? I have multiple labels:
#IBOutlet weak var FL1at0: UIButton!
#IBOutlet weak var FL1at3: UIButton!
#IBOutlet weak var FL1at6: UIButton!
#IBOutlet weak var FL1at9: UIButton!
#IBOutlet weak var FL1at12: UIButton!
#IBOutlet weak var FL1at15: UIButton!
#IBOutlet weak var FL1at18: UIButton!
#IBOutlet weak var FL1at21: UIButton!
Sample Function:
else
{
UserDefaults.standard.set(true, forKey: "RedFl1")
self.FL1at0.setTitleColor(UIColor.red, for: .normal); self.ProgressUpdate()
}
Is there a way to set the table in a way to work like this:
self.FL1at"\(TimeStamp)".setTitleColor(UIColor.red, for: .normal)//TimeStamp comes from function
Try this:
let dict: Dictionary<String, UIButton> = ["FL1at0": FL1at0, "FL1at3": FL1at3, ...]
let x = "FL1at\(TimeStamp)"
if let y = dict[x] as? UIButton {
y.setTitleColor(UIColor.red, for: .normal)
}
You can't.
But you can save buttons in a dictionary:
var dic: [String: UIButton] = ["FL1at0": FL1at0]
then you can use name to access it like:
dic["FL1at\(TimeStamp)"]?.setTitleColorxxxx..

How can I carry out multiple different animations at different times with the same UIButton?

I am creating an animation sequence where when one button is pressed one object fades out. When the same button is pressed again another object fades out. This keeps happening until 8 objects are faded out. I have tried and played around with code and so far have got the code to fade out one object, and two or more at a time, but have not successfully been able to do it as described above. This is my code so far:
import UIKit
class DrinkWaterViewController: UIViewController {
#IBOutlet weak var glassOne: UIImageView!
#IBOutlet weak var glassTwo: UIImageView!
#IBOutlet weak var glassThree: UIImageView!
#IBOutlet weak var glassFour: UIImageView!
#IBOutlet weak var glassFive: UIImageView!
#IBOutlet weak var glassSix: UIImageView!
#IBOutlet weak var glassSeven: UIImageView!
#IBOutlet weak var glassEight: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.glassOne.alpha = 1.0
self.glassTwo.alpha = 1.0
}
#IBAction func drunkOne(_ sender: Any){
if self.glassOne.alpha == 1.0{
UIView.animate(withDuration: 1.5, delay: 0.2, animations:
{self.glassOne.alpha = CGFloat(0.1)}
)
}
}
Put your glasses into an array glasses and add a property to keep track of nextToFade. Then just fade the glasses[nextToFade] and increment nextToFade by 1:
class DrinkWaterViewController: UIViewController {
#IBOutlet weak var glassOne: UIImageView!
#IBOutlet weak var glassTwo: UIImageView!
#IBOutlet weak var glassThree: UIImageView!
#IBOutlet weak var glassFour: UIImageView!
#IBOutlet weak var glassFive: UIImageView!
#IBOutlet weak var glassSix: UIImageView!
#IBOutlet weak var glassSeven: UIImageView!
#IBOutlet weak var glassEight: UIImageView!
// array to hold the glasses so that they can be indexed
var glasses = [UIImageView]()
// index of next glass to fade
var nextToFade = 0
override func viewDidLoad() {
super.viewDidLoad()
// put the glasses into the array
glasses = [glassOne, glassTwo, glassThree, glassFour,
glassFive, glassSix, glassSeven, glassEight]
// make sure they're all visible
glasses.forEach { $0.alpha = 1.0 }
}
#IBAction func drunkOne(_ sender: Any) {
// make sure not to index beyond the end of the array
guard nextToFade < glasses.count else { return }
let glass = glasses[nextToFade]
nextToFade += 1
UIView.animate(withDuration: 1.5, delay: 0.2, animations:
{ glass.alpha = 0.1 }
)
}
}

Reduce number of Outlets? Swift [duplicate]

This question already has answers here:
Swift put multiple IBOutlets in an Array
(5 answers)
Closed 5 years ago.
I've started learning Swift and started off with some simple Games. But every time (depending on the Game) my Code would look like this:
#IBOutlet weak var B1: UIButton!
#IBOutlet weak var B2: UIButton!
#IBOutlet weak var B3: UIButton!
#IBOutlet weak var B4: UIButton!
#IBOutlet weak var B5: UIButton!
#IBOutlet weak var B6: UIButton!
#IBOutlet weak var B7: UIButton!
#IBOutlet weak var B8: UIButton!
#IBOutlet weak var B9: UIButton!
#IBOutlet weak var B10: UIButton!
#IBOutlet weak var B11: UIButton!
#IBOutlet weak var B12: UIButton!
#IBOutlet weak var B13: UIButton!
#IBOutlet weak var B14: UIButton!
#IBOutlet weak var B15: UIButton!
#IBOutlet weak var B16: UIButton!
#IBOutlet weak var B17: UIButton!
#IBOutlet weak var B18: UIButton!
#IBOutlet weak var B19: UIButton!
#IBOutlet weak var B20: UIButton!
#IBOutlet weak var B21: UIButton!
#IBOutlet weak var B22: UIButton!
#IBOutlet weak var B23: UIButton!
#IBOutlet weak var B24: UIButton!
#IBOutlet weak var B25: UIButton!
#IBOutlet weak var B26: UIButton!
#IBOutlet weak var B27: UIButton!
#IBOutlet weak var B28: UIButton!
#IBOutlet weak var B29: UIButton!
#IBOutlet weak var B30: UIButton!
#IBOutlet weak var B31: UIButton!
#IBOutlet weak var B32: UIButton!
#IBOutlet weak var L1: UILabel!
#IBOutlet weak var L2: UILabel!
#IBOutlet weak var L3: UILabel!
#IBOutlet weak var L4: UILabel!
#IBOutlet weak var L5: UILabel!
#IBOutlet weak var L6: UILabel!
#IBOutlet weak var L7: UILabel!
#IBOutlet weak var L8: UILabel!
#IBOutlet weak var L9: UILabel!
#IBOutlet weak var L10: UILabel!
#IBOutlet weak var L11: UILabel!
#IBOutlet weak var L12: UILabel!
#IBOutlet weak var L13: UILabel!
#IBOutlet weak var L14: UILabel!
#IBOutlet weak var L15: UILabel!
#IBOutlet weak var L16: UILabel!
#IBOutlet weak var L17: UILabel!
#IBOutlet weak var L18: UILabel!
#IBOutlet weak var L19: UILabel!
#IBOutlet weak var L20: UILabel!
#IBOutlet weak var L21: UILabel!
#IBOutlet weak var L22: UILabel!
#IBOutlet weak var L23: UILabel!
#IBOutlet weak var L24: UILabel!
#IBOutlet weak var L25: UILabel!
#IBOutlet weak var L26: UILabel!
#IBOutlet weak var L27: UILabel!
#IBOutlet weak var L28: UILabel!
#IBOutlet weak var L29: UILabel!
#IBOutlet weak var L30: UILabel!
#IBOutlet weak var L31: UILabel!
#IBOutlet weak var L32: UILabel!
#IBOutlet weak var C1: UIButton!
#IBOutlet weak var C2: UIButton!
#IBOutlet weak var C3: UIButton!
#IBOutlet weak var C4: UIButton!
#IBOutlet weak var C5: UIButton!
#IBOutlet weak var C6: UIButton!
#IBOutlet weak var C7: UIButton!
#IBOutlet weak var C8: UIButton!
#IBOutlet weak var randomPin1: UIButton!
#IBOutlet weak var randomPin2: UIButton!
#IBOutlet weak var randomPin3: UIButton!
#IBOutlet weak var randomPin4: UIButton!
I am pretty unhappy with this, is there a way to use those Buttons (or whatever type it is) without creating an '#IBOutlet weak var name: Type!'? Like with an identifier and then using the identifier to access a Button? Or is that just the normal way to do this? Thanks in Advance!
You should use IB Outlet Collections and store these instance in arrays:
http://nshipster.com/ibaction-iboutlet-iboutletcollection/
https://medium.com/#abhimuralidharan/what-is-an-iboutletcollection-in-ios-78cfbc4080a1

How to perform the same method on outlets that have similar names instead of writing out very similar lines 10s of times? [duplicate]

This question already has answers here:
How to loop through view outlets in a UIViewController with Swift?
(2 answers)
Closed 6 years ago.
I have these outlets...
#IBOutlet weak var pill1: UIImageView!
#IBOutlet weak var pill2: UIImageView!
#IBOutlet weak var pill3: UIImageView!
#IBOutlet weak var pill4: UIImageView!
#IBOutlet weak var pill5: UIImageView!
#IBOutlet weak var pill6: UIImageView!
#IBOutlet weak var pill7: UIImageView!
#IBOutlet weak var pill8: UIImageView!
#IBOutlet weak var pill9: UIImageView!
#IBOutlet weak var pill10: UIImageView!
I need to hide all of them in the 'viewDidLoad' function. For example...
self.pill1.isHidden = true
self.pill2.isHidden = true
self.pill3.isHidden = true
etc...
etc....all the way to...
self.pill10.isHidden = true
But instead of writing repetitive lines 10s of times that are very similar, how do I use a 'for loop', or whatever is needed, to make it more cleaner.
For example,
for index in 1...10 {
pill(insert index here somehow).isHidden = true
}
I tried a few different ways, but I was getting errors with string types etc. I am new to this all. Any help appreciated. thank you
You can put the views into an array like this:
for pill in [pill1, pill2, pill3, pill4, pill5, pill6, pill7, pill8, pill9, pill10] {
pill.isHidden = true
}
You could consider using an #IBOutlet collection. In that case, all of your outlets would be wired to the same collection (array) variable:
#IBOutlet var pills: [UIImageView]!
for pill in pills {
pill.isHidden = true
}

Array of IBOutlets in Swift

I have code like
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var countryField: UITextField!
#IBOutlet weak var cityField: UITextField!
How can i put all this items to array so i can access each of them in a loop?
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var countryField: UITextField!
#IBOutlet weak var cityField: UITextField!
...
let array = [emailField, countryField, cityField]
Have a look at the documentation on collection types
edit: you can split the declaration/init of that array
// outside any method
var array = [UITextField]?
// in viewDidLoad
self.array = [emailField, countryField, cityField]