简体   繁体   English

没有故事板的 Swift 委托

[英]Swift delegation without storyboard

Hello I have 2 classes :你好,我有 2 个班级:

1st class is a sender that inherits from UIViewController:第一类是从 UIViewController 继承的发送者:

class Sender: UIViewController {

// ReceiverDelegate property for communicating with viewmodel //
var delegate: ReceiverDelegate?

// loginButtonClicked - IBAction called after login button clicked //
@IBAction func loginButtonClicked(sender: AnyObject) {
    delegate?.didPressLoginWithCredentials("xx", password: "ss")

}

override func viewDidLoad() {
    super.viewDidLoad()
    Receiver(vc: self)
    // Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

} }

2nd Class is just Receiver class like a Model and its not a UIViewController:第二类只是像模型这样的接收器类,而不是 UIViewController:

protocol ReceiverDelegate {
func didPressLoginWithCredentials(username: String, password: String)
}


class Receiver: ReceiverDelegate {
init(vc: Sender) {
    vc.delegate = self
}

func didPressLoginWithCredentials(username: String, password: String) {
    println("THE MESSAGE")
}

}

I wanted to ask if thats a good approach of assigning a delegate.我想问一下,这是否是分配代表的好方法。 I need to do it inside my Sender somehow, cause Receiver would never get initialised ?我需要以某种方式在我的 Sender 内部进行,因为 Receiver 永远不会被初始化?

I assigned my delegate in viewDidLoad of Sender.我在 Sender 的 viewDidLoad 中分配了我的委托。

If there is any better approach please help !如果有更好的方法请帮忙! (perhaps i should just do something like var receiver = Receiver() ? and then just call receiver methods without the delegation ?) (也许我应该做类似 var receiver = Receiver() 之类的事情?然后在没有委托的情况下调用接收器方法?)

It should be more like this:它应该更像这样:

class Sender: UIViewController, ReceiverDelegate {

var receiver: Receiver()

override func viewDidLoad() {
    super.viewDidLoad()
    receiver.delegate = self
}

func didPressLoginWithCredentials(username: String, password: String) {
    println("THE MESSAGE")
}

Then in your receiver:然后在您的接收器中:

protocol ReceiverDelegate {
    func didPressLoginWithCredentials(username: String, password: String)
}

class Receiver: NSObject { //or whatever you're subclassing

    weak var delegate: ReceiverDelegate?

    init(vc: Sender) {
        // your initialization code
    }

    // loginButtonClicked - IBAction called after login button clicked 
    @IBAction func loginButtonClicked(sender: AnyObject) {
        delegate?.didPressLoginWithCredentials("xx", password: "ss")
    }
}

Its worth noting your code is a little confusing - i'm not sure what your Receiver is supposed to be, but i've rearranged your code to correctly use protocols/delegation.值得注意的是,您的代码有点令人困惑——我不确定您的接收器应该是什么,但我已经重新安排了您的代码以正确使用协议/委托。 The Receiver will need that login button button in it, so its probably a subclass of UIView. Receiver 将需要其中的登录按钮按钮,因此它可能是 UIView 的子类。 Hard to say without seeing more.很难说没有看到更多。

The code above is what you would do if you have a child view with a button in it, and you want the view controller to handle the action.如果您有一个带有按钮的子视图,并且您希望视图控制器处理操作,那么上面的代码就是您要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM