gpt4 book ai didi

swift - 我的 subview 无法添加到作为 UIView.xib 文件的 subview 的 UIView,它返回 nil

转载 作者:行者123 更新时间:2023-11-30 10:39:30 24 4
gpt4 key购买 nike

我有两个 Swift 文件,一个名为 FileView.swift,另一个是 FileViewController.swift。使用 MVC 架构,我尝试将 FileViewController.swift 中的 subview 添加到 FileView.swift 的 webViewContainer:UIView!这是来自 XIB 文件的 IBOutlet。

但是,当从 FileViewController.swift 调用它时,我得到的结果为零。

class FileView: UIView {

@IBOutlet weak var webViewContainerHeight: NSLayoutConstraint!
@IBOutlet weak var webViewContainerWidth: NSLayoutConstraint!
@IBOutlet weak var closeBttnWidth: NSLayoutConstraint!
@IBOutlet weak var closeBttnHeight: NSLayoutConstraint!
@IBOutlet weak var webViewContainer: UIView!
@IBOutlet weak var closeButton: UIButton!

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

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupNIB()
}

private func setup() {
webViewContainer = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 350))
}
class FileViewController: UIViewController, WKNavigationDelegate {

var webView = WKWebView()
var campaignUrl = ""
var finalCampaignUrl = ""

lazy var customView: FileView = {
let customView = FileView()
return customView
}()

override func loadView() {
self.view = self.customView
}

override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
webViewModal()
}

func webViewModal () {
if UIDevice.current.userInterfaceIdiom == .pad {
let deviceWidth = UIScreen.main.bounds.width/3
let deviceHeight = UIScreen.main.bounds.height/3
customView.webViewContainerWidth.constant = 290 + deviceWidth
customView.webViewContainerHeight.constant = 475 + deviceHeight
customView.closeBttnWidth.constant = 55
customView.closeBttnHeight.constant = 55
customView.closeButton.layoutIfNeeded()
customView.webViewContainer.layoutIfNeeded()
}
webView = Global.setWKWebViewInitWithConfig(frame: .zero)
customView.webViewContainer.addSubview(webView)
customView.webViewContainer.showSpinner(CGSize(width: 30 , height: 30), tintColor: UIColor.lightGray)
webView.translatesAutoresizingMaskIntoConstraints = false
let webViewHeightConstraint = NSLayoutConstraint(
item: webView,
attribute: .height,
relatedBy: .equal,
toItem: customView.webViewContainer,
attribute: .height,
multiplier: 1,
constant: 0
)
let webViewWidthConstraint = NSLayoutConstraint(
item: webView,
attribute: .width,
relatedBy: .equal,
toItem: customView.webViewContainer,
attribute: .width,
multiplier: 1,
constant: 0
)
let webViewLeftMarginConstraint = NSLayoutConstraint(
item: webView,
attribute: .leftMargin,
relatedBy: .equal,
toItem: customView.webViewContainer,
attribute: .leftMargin,
multiplier: 1,
constant: 0
)
let webViewRightMarginConstraint = NSLayoutConstraint(
item: webView,
attribute: .rightMargin,
relatedBy: .equal,
toItem: customView.webViewContainer,
attribute: .rightMargin,
multiplier: 1,
constant: 0
)
let webViewBottomMarginContraint = NSLayoutConstraint(
item: webView,
attribute: .bottomMargin,
relatedBy: .equal,
toItem: customView.webViewContainer,
attribute: .bottomMargin,
multiplier: 1,
constant: 0
)
NSLayoutConstraint.activate([webViewHeightConstraint, webViewWidthConstraint, webViewLeftMarginConstraint, webViewRightMarginConstraint, webViewBottomMarginContraint])

webView.navigationDelegate = self

Global.initialLoadWithParam(ofWebView: webView, withURL: NSURL(string: campaignUrl)!)
customView.closeButton.layer.cornerRadius = 0.9 * customView.closeButton.bounds.size.width
customView.closeButton.backgroundColor = #colorLiteral(red: 0.003921568627, green: 0.1647058824, blue: 0.2666666667, alpha: 1)
customView.closeButton.tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
customView.webViewContainer.layer.cornerRadius = 15
customView.webViewContainer.layer.masksToBounds = true
}


我预计这应该添加

webView = Global.setWKWebViewInitWithConfig(frame: .zero)

customView.webViewContainer.addSubview(webView)

但它在 customView.webViewContainer 的行上返回 nil:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

当在重写函数 loadView() 上加载 customView 时,webViewContainer 不应该已经实例化吗?

最佳答案

问题是你的webViewContainerweak引用:

class FileView: UIView {
// ...
@IBOutlet weak var webViewContainer: UIView!
// ...
}

如果

这工作正常
  • 您正在使用 XIB 文件
  • 某人(例如 View Controller )保留对顶 View 的强引用
  • 导出是 View 层次结构的一部分。

但就你而言,在 setup您创建 View 的实例,将其存储在弱引用中,然后 ARC(引用计数机制)将发现不再有对 UIView 的强引用。因此清除内存,导致 nil引用。

所以你需要以某种方式保留一个强有力的引用;在最简单的情况下,只需跳过 weak导出中的引用说明符。

但请记住,您可能需要执行其他操作来防止强引用循环。就您而言,如果您不使用 xib文件,您可以删除所有 @IBOutlet东西并制作 NSCoder初始化程序只需调用 fatalError ,以防止从 xib 进行任何创建文件。

关于swift - 我的 subview 无法添加到作为 UIView.xib 文件的 subview 的 UIView,它返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57176966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com