没有成员'instantiateWithOwner' [英] does not have a member 'instantiateWithOwner'

查看:550
本文介绍了没有成员'instantiateWithOwner'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像我在之前的问题中所描述的那样制作一个弹出窗口。我实际上得到了答案,但现在我遇到了一个新问题。如果我不创建 instantiateWithOwner ,我可以使视图出现,但它没有响应任何东西(刚刚冻结)。

I'm trying to make a popup like I described in my previous question. I actually got an answer, but now I have a new problem. I can make the view appear if I don't make the instantiateWithOwner, but it is not responding to anything (just frozen).

简而言之,我设置了一个'popup.xib'文件,它只是一个带有按钮和标签的视图。下面的代码应该会在按钮点击时显示和消失。

In short, I have set up a 'popup.xib' file, which is just a view with a button and a label. My code below should make it appear and disappear with button clicks.

我已经阅读了 instantiateWithOwner 所做的文档将视图连接到它的回调按钮的所有魔力,所以当它不在代码中时没有任何反应是有意义的。 (参考

I have read the documentation that the instantiateWithOwner does all the magic of connecting the view to it's callback buttons, so it makes sense that nothing happens when it's not in the code. (reference)

如果我将它包含在我的代码中,我会收到编译器错误 'PopupViewConrtoller'没有名为'instantiateWithOwner'的成员

Thing is that if I do include it in my code, I get a compiler error 'PopupViewConrtoller' does not have a member named 'instantiateWithOwner'.

我试过搜索自动完成列表,但没有发现任何相似之处。

I have tried searching the auto-complete list, but found nothing similar.

我的代码:

ViewController.swift

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBAction func showPopup(sender: AnyObject) {
         // This line makes it appear on the screen, but not respond to anything.
        var x = PickerPopupViewConrtoller(nibName: "PickerPopup", bundle: nil)
         // This line does not compile.
        var x = PickerPopupViewConrtoller(nibName: "PickerPopup", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as? PickerPopupViewConrtoller

        x.show(self.view)
    }
}

PopupViewController

PopupViewController

import UIKit

class PickerPopupViewConrtoller : UIViewController {

    func show(tView : UIView) {
        tView.addSubview(self.view)
    }

    @IBAction func done(sender: AnyObject) {
        self.view.removeFromSuperview()
    }

}


推荐答案

是的,这是正确的, instantiateWithOwner 不是UIViewController方法,它是 UINib 方法。
您必须创建一个UINib对象,然后将其转换为您的UIViewController类

Yes it's correct, instantiateWithOwner is not a UIViewController method, it is a UINib method. You have to create a UINib object and then cast it to your UIViewController class

ex:

UINib( nibName: nibNamed, bundle: bundle).instantiateWithOwner(nil, options: nil)[0] as? UIViewController

这就是为什么我使用我在上一个答案,它更容易,更易读。

That's why I use the extension I wrote in the previous answer, it's easier and more readable.

这篇关于没有成员'instantiateWithOwner'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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