简体   繁体   中英

Custom delegate method in Swift

I am working on an app where I need to have a delegate method for button to identify that which button is pressed by user.Below is the code , please let me know where I am making mistake.Delegate is not working,there is no error,no crash.

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)
}

}

The problem is that you need to assign delegate to object which 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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