gpt4 book ai didi

ios - 添加弹出窗口时出现 NSInternalInconsistencyException

转载 作者:行者123 更新时间:2023-11-30 11:14:59 26 4
gpt4 key购买 nike

我想在 swift 中创建一个弹出窗口,并在用户单击按钮时显示它。我不使用 Storyboard , View 是通过编程创建的。

class CreateNoticePopupViewController: UIViewController {

var popUpView = UIView()

let titleTextField: UITextField = {
let title = UITextField()
title.placeholder = "Enter Title"
title.translatesAutoresizingMaskIntoConstraints = false;
title.font = UIFont.boldSystemFont(ofSize: 24)
return title
}()

let descriptionTextView: UITextView = {
let description = UITextView()
description.translatesAutoresizingMaskIntoConstraints = false;
description.font = UIFont.boldSystemFont(ofSize: 24)
description.isUserInteractionEnabled = true
return description
}()

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
self.popUpView.layer.cornerRadius = 5
self.popUpView.layer.shadowOpacity = 0.8
self.popUpView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
}

func showInView(aView: UIView!, animated: Bool)
{
self.view.addSubview(titleTextField)
self.view.addSubview(descriptionTextView)
aView.addSubview(self.view)

self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]|", options: NSLayoutFormatOptions(),
metrics: nil, views: ["v0": titleTextField]))

self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]|", options: NSLayoutFormatOptions(),
metrics: nil, views: ["v0": descriptionTextView]))

self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-4-[v0]-4-[v1]-4-|", options: NSLayoutFormatOptions(),
metrics: nil, views: ["v0": titleTextField, "v1": descriptionTextView]))

if animated
{
self.showAnimate()
}
}

func showAnimate()
{
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
UIView.animate(withDuration: 0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
});
}

func removeAnimate()
{
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
}, completion:{(finished : Bool) in
if (finished)
{
self.view.removeFromSuperview()
}
});
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

通过按钮我将其称为此 View Controller ,如下所示:

let popViewController = CreateNoticePopupViewController(nibName: "PopUpViewController", bundle: nil)
popViewController.showInView(aView: self.view, animated: true)

然后它给了我这个错误。我正在关注这个链接 https://blog.typpz.com/2015/01/31/ios-sdk-pop-up-window-in-swift/

这是我得到的错误'NSInternalInconsistencyException',原因:'无法在 bundle 中加载NIB:'名为'PopUpViewController'的NSBundle''

最佳答案

如果 VC 是通过编程方式创建的,则可以代替此

let popViewController = CreateNoticePopupViewController(nibName: "PopUpViewController", bundle: nil)

let popViewController = CreateNoticePopupViewController()

关于ios - 添加弹出窗口时出现 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51830584/

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