Swift 4 - Как использовать два viewController с одним файлом Xib? - PullRequest
0 голосов
/ 28 мая 2018

Я хочу знать, как я могу использовать один XIB-файл для двух контроллеров представления.Является ли это возможным?Я провел некоторое исследование, но пока не получил конкретного ответа на этот вопрос.Я надеюсь, что вы можете мне помочь.

1 Ответ

0 голосов
/ 28 мая 2018

Общий 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
    }
}

Нужно установить его размер

enter image description here

enter image description here

Использование

в главном контроллере

///Create a Reference for XIB

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

Использование Чтобы добавить ViewController в качестве Subview

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 в главном контроллере

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...