我正在尝试制作类似于this one的弹出式视图。到目前为止我所做的是:
我的代码:
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBAction func showPopup(sender: AnyObject) {
var x = PopupViewController()
x.show(self.view)
x.showAnimate()
}
}
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%的不透明度),但新视图为空。没有按钮,没有标签,没有不同颜色的区域。
如何让popup.xib显示它的元素?
除了Nerkyator
的回答之外,我错过了文件所有者和主视图之间的连接。只需右键单击“File's Onwer”,然后从视图拖动到主视图下方的两行。
答案 0 :(得分:0)
您需要手动加载与视图关联的xib。 我使用这个扩展名(found at this link)进行了修改以支持UIViewController,然后在需要时调用它。
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
}