【问题标题】:Swift 4- How to use two viewControllers with one Xib file?Swift 4-如何在一个 Xib 文件中使用两个视图控制器?
【发布时间】:2018-05-28 05:34:04
【问题描述】:

我们可以为两个视图控制器使用一个 xib 文件吗?

【问题讨论】:

  • 您可以简单地在两个视图控制器中加载相同的 xib。实际上,这就是xib用于兄弟的用途。可重用性。
  • 我该怎么做?我正在搜索,但不知道程序是什么?
  • xib 是干什么用的?
  • 抱歉,没听懂。 Xib 适用于我的屏幕之一。视图控制器使用相同的屏幕,我想在另一个视图控制器中使用它。
  • 你能把你的xib的图片加进去吗

标签: swift


【解决方案1】:

常用XIB

import UIKit

//MARK:- ConfirmationView
/**
 This Class is used to show a common View for limited time
 */
class ConfirmationView: UIView
{
    //MARK: UIView
    /// Main Content View
    @IBOutlet var contentView: UIView!

    //MARK: UILabel
    /// Title to display in View
    @IBOutlet weak var headerLabel: UILabel!

    //MARK: Init Class
    /**
     This function is used to Initialise the class
     */
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadNib()
    }

    //MARK: Encoder
    /**
     This function is used to Store the class objects added
     */
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        loadNib()
    }

    //MARK: Dienit class
    /**
     This function is used to De-Allocate the class objects
     */
    deinit {
        /// Remove all The Initiated Instances
    }
}

//MARK:- Required Functions
extension ConfirmationView
{
    //MARK: Load Nib
    /**
     This function is used to load the Nib from Bundle
     */
    func loadNib() {
        Bundle.main.loadNibNamed("ConfirmationView", owner: self, options: [:])
        // 2. Adding the 'contentView' to self (self represents the instance of a WeatherView which is a 'UIView').
        addSubview(contentView)
        // 3. Setting this false allows us to set our constraints on the contentView programtically
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.backgroundColor = UIColor.clear
        // 4. Setting the constraints programatically
        contentView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        contentView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        contentView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        contentView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    }
}

需要设置它的大小

用法

在主控制器中

///Create a Reference for XIB

/// Confirmation View Xib Class Reference
private var confirmationViewXib : ConfirmationView?

用于在 ViewController 中添加为子视图

confirmationViewXib = self.showConfirmationView(staticText: "Sign Up Completed using \(loginType)")
self.view.addSubview(confirmationViewXib!)

显示方法

//MARK: Show ConfirmatonView
/**
  This function is used show the confirmationView Xib with text Passing
 */
func showConfirmationView(staticText:String) -> ConfirmationView
{
    let newView = ConfirmationView.init(frame: self.view.bounds)
    newView.headerLabel.text = staticText
    return newView        
}

注意:实现委托或观察者从主控制器中的 XIB 获取输出

【讨论】:

    猜你喜欢
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2014-07-25
    • 2014-09-03
    相关资源
    最近更新 更多