为什么我的弹出视图是空的? [英] Why is my popup view is empty?

查看:31
本文介绍了为什么我的弹出视图是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个类似于这个的弹出式视图.到目前为止,我所做的是:

I am trying to make a popup view similar to this one. What I have done so far is:

  • 打开一个新的单一视图项目.
  • 在主视图中添加了一个按钮.
  • 添加了一个名为popup.xib"的新 xib 文件
  • 添加了一个名为PopupViewController.swift"的新 swift 文件
  • 在身份选项卡上,我将第一响应者类设为PopupViewController"
  • 我在 popup.xib 中放入了一个标签、一个按钮和一个具有不同背景颜色的视图.当然,一切都对其出现的位置有限制.

我的代码:

ViewController.swift

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBAction func showPopup(sender: AnyObject) {
        var x = PopupViewController()
        x.show(self.view)
        x.showAnimate()
    }
}

PopupViewController

PopupViewController

import UIKit

class PopupViewController : UIViewController {

    func show(tView : UIView) {
        tView.addSubview(self.view)
        println("here")
        self.view.backgroundColor = UIColor.redColor()

    }

    func showAnimate() {
        self.view.transform = CGAffineTransformMakeScale(1.0, 1.0)
        self.view.alpha = 0.3
    }

}

结果:当按下按钮时,我会在视图上看到一个红色的叠加层(因为我添加的视图是红色的并且具有 30% 的不透明度),但新视图是空的.没有按钮,没有标签,没有不同颜色的区域.

The result: When the button is pressed I get a redish overlay on the view (because the view I added is red and has 30% opacity), but the new view is empty. No button, no label, no area with different colour.

我需要做什么才能让 popup.xib 显示它的元素?

What do I have to do to make the popup.xib show it's elements?

除了 Nerkyator 的回答之外,我还缺少文件所有者和主视图之间的连接.只需右键单击File's Onwer",然后从视图拖动到其下方两行的主视图.

I was missing the connection between the File's Owner and the main view in addition to Nerkyators answer. Just right click the "File's Onwer" and from view drag to the main view that is two lines below it.

推荐答案

您需要手动加载与您的视图关联的 xib.我使用此扩展(在此链接中找到)修改为支持 UIViewController然后我在需要时调用它.

You need to load manually the xib associated to your view. I use this extensions (found at this link) modified to support UIViewController and then I call it when needed.

extension UIViewController {
    class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> Self? {
        return UINib(
            nibName: nibNamed,
            bundle: bundle
            ).instantiateWithOwner(nil, options: nil)[0] as? UIViewController
    }
}

然后

@IBAction func showPopup(sender: AnyObject) {
    var x = PopupViewController.loadFromNibNamed("popup")
    //do what you need with x
}

这篇关于为什么我的弹出视图是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆