Custom Delegation Method in Swift

I am working on an application where I need to have a delegate method for a button in order to determine which button is clicked by the user. Below is the code, please let me know where I am doing the error. The failure does not work, there is no error, no glitches.

import UIKit
@objc protocol SAlertViewDelegate {

optional func clickedButtonTitle(title:String)
}

class CustomAlertView: UIView {

var delegate:SAlertViewDelegate?
 func button1Action(sender: UIButton?) {

    let titleLabel=sender?.titleLabel?.text
    self.delegate?.clickedButtonTitle!(titleLabel!)
    removeFromMainView()
}

}

      

View Controller: -

import UIKit

class CurrentImageVC: UIViewController,SAlertViewDelegate
{
var alert:CustomAlertView=CustomAlertView()
override func viewDidLoad() {
    super.viewDidLoad()

    alert.delegate=self

    }

//MARK: Custom Delegate
func clickedButtonTitle(title:String)
{
    println(title)
}

}

      

+3


source to share


1 answer


The problem is that you need to assign a delegate to the object you are using to add the subview.



let alertSubView: CustomAlertView = CustomAlertView()
alertSubView.frame=CGRectMake(0,0,1024,768)
alertSubView.delegate=self
self.view.addSubview(alertSubView)

      

+1


source







All Articles