First, solve the problem
Xib/storybarod can be conveniently and visually set constraints, and is becoming more and more important in development. Because Xib cannot be packaged, encapsulation and reuse become impractical. This article describes a solution to implement xib components.
Second, the model block principle
Before we introduce the principles, we will first clarify two concepts:
As you can see, the file's owner and root view view are selected, each with the custom class Properties panel. What is the function of the class attribute, and what is the difference?
2.1 View's Class Property
The class property of the view is used to specify the instantiated class for the selected view. Xib is actually an XML file that, when loaded, creates and sets the view instance based on the XML content. The class here is the instance that tells the parsing logic what class you want to create. If set to UIButton here, the parsing logic generates an instance of UIButton.
2.2 The Class property of File ' s owner
Feile's owner's Class attribute, in most cases, is **uiviewcontroller** and its subclasses.
1 |
public func loadNibNamed(name: String!, owner: AnyObject!, options: [NSObject : AnyObject]!) -> [AnyObject]! |
As can be seen from the load interface above xib, loading xib needs to specify an instance of the owner class * *, the parsing logic does not create a new instance like **2.1**, but instead uses a created instance with the parameter name owner.
If not, why would you specify the class property of file ' owner?
The value of the class attribute set here is the main function of the keyword @iboutlet, declaring which properties and methods can establish an association relationship. The parsing logic assigns a reference to the associated view to the corresponding property of owner, and the trigger event executes the Owner.method () method. In order to handle the business logic related to the interface conveniently in owner. As can be understood, File's owner's class, is the associated interface declaration, loadnibnamed the incoming owner is the implementation.
Tips
file's Owner's Class attribute, a declarative function that informs which properties and methods can be used.
12345678 |
class ilviewcontroller: uiviewcontroller {       @IBOutlet weak var label: uilabel! } class ilflagcontroller: uiviewcontroller {      @IBOutlet weak var label: uilabel!      } |
In this case (as in the above code), when loading xib using the Loadnibnamed method, the owner parameter is passed into the Ilviewcontroller instance, and the class of file ' s owner in Xib is set to Ilflagcontroller, is it feasible? Answer: it works.
Modular principle of 2.4 xib
In Storybarod/xib, only the class property of the view is related to the component. The view is created by the Xib parsing logic, so to implement the component, it is necessary to automatically execute the function of the loading sub-xib module when this class is instantiated.
Third, the source of tool class
In order to achieve xib modularity, a small functional class is required:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
import UIKit
@objc class ILXibView: UIView {
@IBOutlet
var
contentView: UIView!
override init(frame: CGRect) {
super
.init(frame: frame)
self.loadView()
}
required init?(coder aDecoder: NSCoder) {
super
.init(coder: aDecoder)
}
override func awakeFromNib() {
super
.awakeFromNib()
self.loadView()
}
private func getXibName() -> String {
let clzzName = NSStringFromClass(self.classForCoder)
let nameArray = clzzName.componentsSeparatedByString(
"."
)
var
xibName = nameArray[0]
if
nameArray.count == 2 {
xibName = nameArray[1]
}
return
xibName
}
func loadView() {
if self.contentView != nil {
return
}
self.contentView = self.loadViewWithNibName(self.getXibName(), owner: self)
self.contentView.frame = self.bounds
self.contentView.backgroundColor = UIColor.clearColor()
self.addSubview(self.contentView)
}
private func loadViewWithNibName(fileName: String, owner: AnyObject) -> UIView {
let nibs = NSBundle.mainBundle().loadNibNamed(fileName, owner: owner, options: nil)
return
nibs[0] as! UIView
}
}
|
Iv. Examples of actual combat
4.1 Package Xib Components
Create a new ildemoview.xib, ildemoview.swift two files (with the same file name) and set the Ildemoview file's owner's class to Ildemoview
123456789 |
class ILDemoView: ILXibView {
@IBOutlet weak
var
label: UILabel!
override func awakeFromNib() {
//一定要调用super
super
.awakeFromNib()
self.label.text =
"你好,中国"
}
}
|
Add Uilabel to the Xib file and associate to Ildemoview
4.2 Using the Xib component
Create a new Xib/storyboard file, add a UIView control, and set the class property of this control to Ildemoview
Tips
When using, set the target UIView class property to Ildemoview, and then drag the UIView control to establish an association relationship, you will find that the property type is automatically set to Ildemoview in this code. Ilxibview simple but very practical, our project has been a lot of use of it, for the XIB modular package, is absolutely a tool.
Download Demo: Xib modular design of Http://00red.com/download/Swift/ilxibdemo.zip
The Xib modular design of Swift