I want to declare a global array of NSTextFields, but "Class ViewController has no initializers." Yes, global, but static.
class ViewController: NSViewController {
#IBOutlet weak var TextField01: NSTextField!
#IBOutlet weak var TextField02: NSTextField!
#IBOutlet weak var TextField03: NSTextField!
var fieldArray: [NSTextField]
override func viewDidLoad() {
super.viewDidLoad()
fieldArray = [TextField01, TextField02, TextField03]
}
I thought that the problem was the compiler not knowing about the main storyboard; that's why I didn't assign values to my fieldArray. It would appear that I need to declare (initialize?) fieldArray in some other way.
Related
I'm adding a new TableVIewto the project and I'm also creating a custom class fro the cell. I'm doing as usual : New file/ Cocoa Touch Class / UITableViewCell / name. As soon as I start adding properties I get the error dough properties are declared as!. It doesn't happen on my other custom cell class. Can you see what am I doing wrong with this new class?
No error on this class :
import UIKit
class CalendarTableViewCell: UITableViewCell {
#IBOutlet weak var dayLabel: UILabel!
var cellId: String!
var cellWeekday: Int!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
super.prepareForReuse()
// Set your default background color, title color etc
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
and the new class that makes xCode complain:
import UIKit
class ProductTableViewCell: UITableViewCell {
#IBOutlet weak var productImageView: UIImageView!
#IBOutlet weak var productIDLabel: UILabel!
#IBOutlet weak var productIDInfoLabel: UILabel!
#IBOutlet weak var categoryLabel: UILabel!
#IBOutlet weak var categoryInfoLabel: UILabel!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var nameInfoLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var priceInfoLabel: UILabel!
#IBOutlet weak var quantityLabel: UILabel!
#IBOutlet weak var quantityInfoLabel: UILabel!
var productImage: UIImage!
var category: String!
var productId: String!
var name: String!
var price: String
var vendor: String!
var cellId: Int64
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
It's something I overlooked for sure but I can't spot it.
What should I check?
Your var cellId: Int64 is not initialized. In first cell you explicitly specified that you will initialize it before use with exclamation mark, but not in the second cell.
I want change #IBOutlet text value from another class
How i can set #IBOutlet to static ?
Below code not work:
#IBOutlet internal static var boxGender: UIView!
You can set a static value of your controller and get all values from them. But this is not a right way to reach your outlets. You should pass your controller to other class instance.
class Test: UIViewController {
var boxGender: UIView!
static var instance: Test?
override func viewDidLoad() {
super.viewDidLoad()
Test.instance = self
}
}
Test.instance?.boxGender
An IBOutlet cannot be static. (The compiler will give the error Only instance properties can be declared #IBOutlet)
class SomeViewWithALabel: UIView {
#IBOutlet weak var myLabel: UILabel!
// ... methods and properties
}
class MyController: UIViewController {
#IBOutlet weak var someViewWithALabel: SomeViewWithALabel!
//...
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
someViewWithALabel?.myLabel.text == "Custom text"
}
}
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
}
I'm trying to make an app to count different tokens in a board game. I thought it would be easy peasy, but not quite.
I succeeded in adding one UIStepper to count one kind of token, but I don't know how to add the other four. This is what I've got so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func StepperTapped(sender: UIStepper) {
self.theLabel.text = "\(self.stepper.value)"
}
}
I got one action and two outlets.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var stepper: UIStepper!
#IBOutlet weak var theLabel2: UILabel!
#IBOutlet weak var stepper2: UIStepper!
#IBOutlet weak var theLabel3: UILabel!
#IBOutlet weak var stepper3: UIStepper!
#IBOutlet weak var theLabel4: UILabel!
#IBOutlet weak var stepper4: UIStepper!
#IBOutlet weak var theLabel5: UILabel!
#IBOutlet weak var stepper5: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func StepperTapped(sender: UIStepper) {
self.theLabel.text = "\(self.stepper.value)"
}
#IBAction func StepperTapped2(sender: UIStepper) {
self.theLabel2.text = "\(self.stepper2.value)"
}
#IBAction func StepperTapped3(sender: UIStepper) {
self.theLabel3.text = "\(self.stepper3.value)"
}
#IBAction func StepperTapped4(sender: UIStepper) {
self.theLabel4.text = "\(self.stepper4.value)"
}
#IBAction func StepperTapped5(sender: UIStepper) {
self.theLabel5.text = "\(self.stepper5.value)"
}
}
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]