I have my UIButton Outlet in UiView.xib file and I want to create an action/function which will change the text of the UILablel Outlet which is from another UIView.xib file.
I have already add both UIViews in my ViewController file but I have no idea how to create function to access the elements from the differnt XIB files and call them in function.
Here is my code of the ViewController file
override func viewDidLoad() {
super.viewDidLoad()
// UIView 1 (XIB File 1)
if let addUIView1 = Bundle.main.loadNibNamed("totalOz", owner: self, options: nil)?.first as? totalOZVC {
self.view.addSubview(addUIView1)
// Button 120ml function
addUIView1.button120ml.addTarget(self, action: #selector(ViewController.buttonSelectGlassSize(sender:)), for: .touchUpInside)
}
// UIView2 (XIB File 2)
if let addUIView2 = Bundle.main.loadNibNamed("GlassSelector", owner: self, options: nil)?.first as? GlassSelectorVC {
self.view.addSubview(addUIView2)
addUIView2.labelGlassSelect.textColor = UIColor.white
addUIView2.labelGlassSelect.text = "Select the size of Glass"
}
// Creating a function to call the Outlets from the XIB files
@objc func buttonSelectGlassSize(sender: UIButton) {
// Change the text of labelGlassSelect when user press the button 120mL
}
Sorry I am still very new to Swift, please can you guys help me out.
Thank you so much!
Made the global instance of addUIView1
and addUIView2
. In the buttonSelectGlassSize
method, access the addUIView2 and update the text of label. Currently you have not global instance, so you are not able to update the label's text.
To call functions from different xibs back to your controller, use custom delegates methods. Below is tutorial
Implementing delegates in Swift, step by step.
To update other class data from your controller make internal functions in other class and call them from your controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.