如何在 placeholderView 的 ViewController 中加载笔尖

How to load nib in a ViewController on a placeholderView

我有一个具有 placeholder 视图的视图控制器,我还有一个自定义 nib,我想将其添加到该 placeHolderView。我正在努力弄清楚如何在 viewController 中注册笔尖,以便当我推送到此视图控制器时,占位符视图将替换为我的笔尖。

这是我尝试过的:

CountryViewController

import UIKit

class CountryViewController: UIViewController {

    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var flagImage: UIImageView!
    @IBOutlet weak var countryInfoViewPlaceHolder: UIView!

    private let countryDetails: CountryData

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpNavBar(countryName: countryDetails.name)
        addSubView()
    }

    init(countryDetails: CountryData) {
        self.countryDetails = countryDetails
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        return nil
    }


    func setUpNavBar(countryName: String) {
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        self.title = countryName
        self.navigationController?.navigationBar.tintColor = UIColor.countryThemeGrey()
    }

    func addSubView() {
        guard let nibView = Bundle.main.loadNibNamed("CountryDetailsView", owner: self, options: nil)?.first as? CountryDetailsView else {
            return
        }
        countryInfoViewPlaceHolder.addSubview(nibView)
    }
}

国家/地区详细信息视图

class CountryDetailsView: UIView {

    @IBOutlet weak var capitalLabel: UILabel!
    @IBOutlet weak var regionLabel: UILabel!
    @IBOutlet weak var subRegionLabel: UILabel!

    convenience init() {
        self.init(frame: .zero)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        return nil
    }

    private func commonInit() {
//        let nib = UINib(nibName: String(describing: type(of: self)), bundle: nil)
//        contentView = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
//        addSubviewPinnedToEdges(contentView)
        Bundle(for: CountryDetailsView.self).loadNibNamed("CountryDetailsView", owner: self, options: nil)
    }

    func setViewWithData(country: CountryData) {
        capitalLabel.text = country.capital
        regionLabel.text = country.region
        subRegionLabel.text = country.subRegion
    }

}

您可以添加静态方法。喜欢下面

class CountryDetailsView: UIView {

@IBOutlet weak var capitalLabel: UILabel!
@IBOutlet weak var regionLabel: UILabel!
@IBOutlet weak var subRegionLabel: UILabel!

static func initView() -> CountryDetailsView {
    return Bundle(for: CountryDetailsView.self).loadNibNamed(String(describing: CountryDetailsView.self), owner: self, options: nil)!.first as! CountryDetailsView
}

func setViewWithData(country: CountryData) {
    capitalLabel.text = country.capital
    regionLabel.text = country.region
    subRegionLabel.text = country.subRegion
}
}

使用:

func addSubView() {
    let nibView = CountryDetailsView.initView()
    countryInfoViewPlaceHolder.addSubview(nibView)
}