如何设置UIView委托?

时间:2016-03-03 09:04:02

标签: ios swift uiview delegates protocols

我有一个UIViewController

class WelcomeViewController: UIViewController

和UIView

class SignUpView: UIView

现在我要设置WelcomeViewController SignUpView代表{/ 1}}

protocol SegueDelegate {
    func runSegue(identifier: String)
}

class SignUpView: UIView { ... }

并在

中连接
class WelcomeViewController: UIViewController, SegueDelegate {

如何在WelcomeViiewController中设置那些委托?当我试图设置时:

override func viewDidLoad() {
    SignUpView.delegate = self
}

它让我回头

Instance member 'delegate' cannot be used on type 'SignUpView'

我该如何找到解决方案?

5 个答案:

答案 0 :(得分:3)

您正在尝试将委托设置为类。它应该是该类的一个实例,即

let signUpView = SignUpView()
signUpView.delegate = self

答案 1 :(得分:0)

这样做有什么意义?如果您想从一个View导航到另一个View,只需在Storyboard中添加带有标识符的Segue,这样您就可以调用self.performSegueWithIdentifier("IdentifierOfSegue", sender: self)

答案 2 :(得分:0)

在该委托(协议)的SignUpView中创建一个弱属性,并将其命名为委托以外的其他

然后你可以设置和使用它。

答案 3 :(得分:0)

我同意开发人员说'#34;你可以通过segue"但 问题是你没有在SignUpView类中声明委托var 所以你可以在signIn中实现它,如果你声明它,请在评论中为我编写代码行来检查它 目前 ... 我建议您将子视图设为父类,然后覆盖 你想要调用哪种方法 并且您需要将委托var声明为可选(因此您不会拥有 一个记忆周期),如下面一行...

var delegate: SegueDelegate?

答案 4 :(得分:0)

让我们为需要解决此问题时需要解决方案的有需要的人解决这个问题:

在你的UIView中:

This means interesting things like using your Thunderbolt 3 desktop as an external graphics card for your laptop should be possible. This would be implemented by exposing the GPU to the Thunderbolt connection as if it were a VM running on the desktop (using the IOMMU).

你需要添加:

class SignUpView: UIView

现在,仍然在您的班级var delegate : SegueDelegate? 中,您需要添加您想要委派的功能,就像这样:

SignUpView

这将致电您的代表:

func runSegue(identifier: String) {
    delegate?.runSegue(identifier)
}

现在,在您的protocol SegueDelegate { func runSegue(identifier: String) } 中,您应该将ViewController放在某处(通过编程方式创建或通过Storyboard / XIB链接)。

SignUpView功能中,添加:viewDidLoad

不要忘记在班级遗产中添加signUpView.delegate = self