Swift: set #IBOutlet static - swift

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

Related

global declaration of NSTextField array

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.

Using Delegation to Pass Data from the Initial View Controller to a Second View Controller - Swift

I am trying to understand delegation in Swift better, and I have seen how delegation can be used to pass data from a second view controller back to an initial view controller. But I am confused about how to pass data from the initial view to a second view using delegation. I have initialized an instance of the delegate in my initial view and implement the delegate protocol in the second view, but I am still unsuccessful in passing the data from initial view to second view using delegation. Any advice on what I might be doing wrong would be appreciated! Here is the storyboard view of what my delegation attempt looks like. 1
Initial View Class:
import UIKit
class ViewController: UIViewController {
var data: DataDelegate?
#IBOutlet weak var text: UITextField!
#IBOutlet weak var send: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func send(_ sender: Any) {
let msg = text.text!
data?.sendData(data: msg)
print("SENDING MESSAGE: \(msg)")
}
}
protocol DataDelegate {
func sendData(data: String)
}
Second View Class:
import UIKit
class RVC: UIViewController, DataDelegate {
#IBOutlet weak var delegationLabel: UILabel!
#IBOutlet weak var message: UILabel!
#IBOutlet weak var back: UIButton!
let vc = ViewController()
func sendData(data: String) {
print("DATA SENT")
let msg = data
print(msg)
message.text = data
}
override func viewDidLoad() {
super.viewDidLoad()
print("LOADED")
vc.data = self
// Do any additional setup after loading the view.
}
}

How to use functions from other swift files (same target) in same project

Hey I have 2 Classes each in own swift file. Im basically doing this only to get a better overview in my Viewcontroller. Still, I need to get access to the data or call the func from other files in my view controller.swift file. Its in the same Target so I don't need to import it in my view controller file right?
If I do override the func viewDidLoad() I get an Exception so I guess I am only allowed to do it once (-> in my viewController.swift)
//ViewController.swift
class ViewController: UIViewController{
#IBOutlet weak var xMotion: UILabel!
#IBOutlet weak var yMotion: UILabel!
#IBOutlet weak var zMotion: UILabel!
#IBOutlet weak var lineChartView: LineChartView!
#IBOutlet weak var lineChartView2: LineChartView!
override func viewDidLoad() {
super.viewDidLoad()
timebuffer.append(Double(ts1))
colors.append(UIColor.red)
Graphen.customizeChart(values: buffer1.map { Double($0) })
Graphen.filteredChart(values: buffer2.map { Double($0) })
Graphen.multipleCharts()
}
//Graphen.swift
class Graphen : ViewController
{
//creates Plot with specific numbers/data
func customizeChart(values: [Double]){
var dataEntries: [ChartDataEntry] = []
for i in 0..<buffer1.count{//dataPoints.count
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry) }
lineChartDataSet = LineChartDataSet(entries: dataEntries, label: nil)
lineChartDataSet.circleRadius = 0.5
let lineChartData = LineChartData(dataSet: lineChartDataSet)
self.lineChartView.data = lineChartData
}
}
viewDidLoad() is a lifecycle method of a UIViewController's instance. Thus, it can be overridden in each UIViewCiontroller subclass, i.e.
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
//your code here...
}
}
class Graphen : ViewController
{
override func viewDidLoad() {
super.viewDidLoad()
//your code here...
}
}
Now, you don't need to import any file/class as long as they are in the same target.
Now, since Graphen's customizeChart(values:) method is an instance method, so you need to create an instance of Graphen first and then use it to call its instance methods like so,
let graphen = Graphen()
graphen.customizeChart(values: buffer1.map { Double($0) })
Similarly call other instance methods of Graphen. Assuming that filteredChart(values:) and multipleCharts() are also instance methods, you can invoke them like,
graphen.filteredChart(values: buffer2.map { Double($0) })
graphen.multipleCharts()

Custom UITableViewCell has no initialiser error on only one of the two created classes. Swift

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.

How to pass data from a parent view controller to its segmented control views in Swift?

I have a view controller with three segmented view segments. each segment has containers. I want to pass data from the parent view to its segmented views..
class UserDetails: UIViewController {
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var view1: UIView!
#IBOutlet weak var view2: UIView!
#IBOutlet weak var view3: UIView!
var id = "120"
i want to pass this variable "id" to view1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view1.hidden = false
view2.hidden = true
view3.hidden = true
print(self.id)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I want to pass variable id to view1..