Ios Swift中的自定义委托方法
我正在开发一个应用程序,在这个应用程序中,我需要一个按钮的委托方法来识别用户按下了哪个按钮。下面是代码,请让我知道我在哪里出错。委托不工作,没有错误,没有崩溃Ios Swift中的自定义委托方法,ios,swift,Ios,Swift,我正在开发一个应用程序,在这个应用程序中,我需要一个按钮的委托方法来识别用户按下了哪个按钮。下面是代码,请让我知道我在哪里出错。委托不工作,没有错误,没有崩溃 import UIKit @objc protocol SAlertViewDelegate { optional func clickedButtonTitle(title:String) } class CustomAlertView: UIView { var delegate:SAlertViewDelegate? fun
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()
}
}
视图控制器:-
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)
}
}
问题是您需要将委托指定给用于添加子视图的对象
let alertSubView: CustomAlertView = CustomAlertView()
alertSubView.frame=CGRectMake(0,0,1024,768)
alertSubView.delegate=self
self.view.addSubview(alertSubView)
您如何展示您的CustomAlertView?让alertSubView=CustomAlertView()alertSubView.frame=CGRectMake(0,01024768)self.view.addSubview(alertSubView)谢谢伙计,您节省了我的时间:-)欢迎@Saurabh如果您有任何其他问题,请告诉我