簡體   English   中英

如何在 swift 中單擊按鈕時隱藏/顯示 Xib 視圖

[英]How to hide/show Xib view on button click in swift

我以編程方式創建了 xib 視圖(AdvertisementView),在這里我為按鈕添加了委托方法

import UIKit
protocol SampleButtonViewDelegate: AnyObject {
func sampleButtonTapped()
}
class AdvertisementView: UIView {
// MARK: - MySubViews
//some other view...
lazy var closeButton: UIButton = {
    let buttonClose = UIButton(type: .roundedRect)
    buttonClose.translatesAutoresizingMaskIntoConstraints = false
    buttonClose.backgroundColor = .red
    buttonClose.setTitle("X", for: .normal)
    buttonClose.addAction {
        print("this is close button")
        self.delegate?.sampleButtonTapped()
    }
    return buttonClose
}()

// MARK: - Properties
var website = ""
weak var delegate: SampleButtonViewDelegate?

// MARK: - Initializers
override init(frame: CGRect) {
    super.init(frame: frame)
    clipsToBounds = true
    addCloseButton()
}
required init?(coder: NSCoder) {
    super.init(coder: coder)
    clipsToBounds = true
    addCloseButton()
}
private func addCloseButton() {
    addSubview(closeButton)
    NSLayoutConstraint.activate([
        closeButton.topAnchor.constraint(equalTo: titleLabel.topAnchor),
        closeButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 15),
        closeButton.widthAnchor.constraint(equalToConstant: 35),
        closeButton.heightAnchor.constraint(equalToConstant: 25)
    ])
}
}

在 global.swift 文件中,我已將 AdvertisementView 添加到 containerview --- 這是全局文件

let adView = AdvertisementView()

func addAdvertisementView(containerView: UIView) {
//    let adView = AdvertisementView()
containerView.isHidden = false
containerView.clipsToBounds = true
containerView.addSubview(adView)
adView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    adView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 5),
    adView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -5),
    adView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 5),
    adView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -5)
])
}

在父 class 中,我在 storyboard 中添加了視圖,並將其 IBOutlet 作為adContainerView :使用此代碼,我可以在單擊關閉按鈕時關閉adContainerView

關閉后,如果我從消息列表視圖 controller 移動並再次返回,我想adContainerView with adView但我只得到adContainerView ,但最重要的是adView設計沒有出現,為什么?

如何使用 adView 顯示 adContainerView `...請指導我

class 消息列表:UIViewController,SampleButtonViewDelegate {

@IBOutlet weak var adContainerView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    addAdvertisementView(containerView: adContainerView)
    adView.delegate = self
}
func sampleButtonTapped() {
    adContainerView.isHidden = true
}
}

當您移動到消息列表視圖 controller 時,之前的視圖不是 deinit。 它只是隱藏,所以您設置的所有屬性仍然保留在那里。 Apple 稱之為 View Controller 生命周期。

就像您描述的那樣,在這里 adContainerView 仍然被 func sampleButtonTapped() 隱藏

因此,如果您需要在從另一個視圖返回時取消隱藏它。 您在 viewWillAppear 中顯示檢查它,因為視圖將再次出現而不是從初始化

代碼將是這樣的 - 您可以添加更多條件來檢查是否需要取消隱藏它

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    
    if (adContainerView.isHidden) {
        adContainerView.isHidden = false
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM