Recently I am studying the book "iOS 12 Programming Fundamentals with Swift". Under the section "manual Nib Loading", readers are supposed to be able to load a nib using .xib file. I followed the three steps: load the nib (which is the .xib file), obtain the instances that it creates as it loads, and do something with those instances. But I cannot see the designed view shown in the running app's interface. Any possibility why this is so?
ps The name of the .xib file is View.xib.
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let arr = Bundle.main.loadNibNamed("View", owner: nil)!
let v = arr[0] as! UIView
self.view.addSubview(v)
}
}
try
let view = Bundle.main.loadNibNamed("View", owner: nil, options: nil)?[0] as! UIView
self.view.addSubview(view);
I found the reason why I failed to load the .xib file. In Main.storyboard, under the Custom Class in the Identity Inspector, I should set the Class value as the name of the .swift file which is used to instantiate the view controller, "ViewController", instead of the default value "UIViewController".
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.