Variable UILabel name - swift

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..

Related

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

Can I use a tablecell which is containing in another tableview(from storyboard)?

Here is the cell I want to reuse.
And there is a related cell class.
class FlightTransitTBCell: UITableViewCell {
#IBOutlet weak var abovebullet: UILabel!
#IBOutlet weak var bottomBullet: UILabel!
#IBOutlet weak var upBullet: UILabel!
#IBOutlet weak var downBullet: UILabel!
#IBOutlet weak var lblAboveTime: UILabel!
#IBOutlet weak var lblButtomTime: UILabel!
#IBOutlet weak var lblUpTime: UILabel!
#IBOutlet weak var lblDownTime: UILabel!
#IBOutlet weak var lblAboveAirPort: UILabel!
#IBOutlet weak var lblButtomAirPort: UILabel!
#IBOutlet weak var lblDownAirPort: UILabel!
#IBOutlet weak var lblUpAirPort: UILabel!
#IBOutlet weak var viaDown: UILabel!
#IBOutlet weak var viaUp: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
downBullet.text = "\u{2022}"
upBullet.text = "\u{2022}"
abovebullet.text = "\u{2022}"
bottomBullet.text = "\u{2022}"
downBullet.font = UIFont(name: getAppFont(), size: 20)
upBullet.font = UIFont(name: getAppFont(), size: 20)
abovebullet.font = UIFont(name: getAppFont(), size: 40)
bottomBullet.font = UIFont(name: getAppFont(), size: 40)
}
}
Following is my xib, which contain a tableview that I'm trying to display above cell.
And I register the cell from the xib class like this.
tableView.register(FlightTransitTBCell.self, forCellReuseIdentifier: "TransitCell")
then dequeue from tableview like this..
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let transitCell = tableView.dequeueReusableCell(withIdentifier: "TransitCell") as? FlightTransitTBCell{
transitCell.awakeFromNib()
transitCell.selectionStyle = .none
transitCell.setTransitView(indexPath.row, firstRoute, lastRoute)
transitCell.setBullet(outward_[indexPath.row])
if indexPath.row == 2 {
transitCell.backgroundColor = UIColor.green
}
return transitCell
}
return UITableViewCell()
}
I got the cell and it means my registration is working. But all my outlets are being nil and giving me error. I don't know what I'm doing is proper way of not. I'm not sure. This is my first time of doing like that and stuck here.

cannot assign to property: 'images' is a get-only property

import UIKit
class Categorycell: UITableViewCell {
#IBOutlet weak var categoryImage : UIImage!
#IBOutlet weak var categoryTitle : UILabel!
func updateViews(category: category) {
categoryImage.images = UIImage(named: category.imageName)
categoryTitle.text = category.title
}
}
Try to change #IBOutlet weak var categoryImage : UIImage!
to #IBOutlet weak var categoryImage : UIImageView!

Making a button save to a variable

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?

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]