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