NSInternalInconsistencyException在添加弹出窗口时

时间:2018-08-13 21:09:02

标签: ios swift

我想快速创建一个弹出窗口,并在用户单击按钮时显示它。 我不使用情节提要,并且以编程方式创建了视图。

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")
}
}

然后从按钮中将其称为此视图控制器,如下所示:

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',原因:'无法在捆绑包中加载NIB:'名称为'PopUpViewController'的NSBundle

1 个答案:

答案 0 :(得分:0)

如果以编程方式创建了VC,则代替此方式

<div class="grid">
  <div class="column">
  <div class="element higher">1</div>
  <div class="element">2</div>
  </div>
   <div class="column">
  <div class="element">3</div>
  <div class="element">4</div>
  </div>
</div>


.grid {
 display:flex;
 flex-wrap:wrap;
 flex-direction:row;
 margin:0 auto;
 width:230px;
 border:1px solid blue;  
}

.column {
  display: flex;
  flex-direction: column;
}

 .element {
   width:100px;
   height:140px;
   margin:5px;
   background: red;
 }

 .higher {
   height:160px;
 }

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