ios - 如何将目标添加到扩展中的按钮?
问题描述
如果我的视图控制器为空,我编写了一个扩展来显示空状态。我还想添加一个带有目标的UIButton() 。如何在扩展中为我的按钮实现addTarget操作?(Fe 一个简单的“Hello World”或我需要的任何东西)
extension UITableView{
func setEmptyView(title: String, message: String) {
// handling states
button.setTitle("+ Add New Documentation", for: .normal)
button.titleLabel!.font = UIFont(name: "Roboto-Regular", size: 24)
button.setTitleColor(UIColor(hex: "BDC86F"), for: .normal)
button.backgroundColor = UIColor(hex: "506182")
button.addTarget...
//more code
emptyView.addSubview(button)
}
}
解决方案
我已经实现了同样的UIView
扩展。希望,它会帮助你,我假设你能够将它转换为UITableView
代码:
extension UIView{
func setEmptyView(title: String, message: String) {
let emptyView = UIView(frame: self.bounds)
// handling states
let button = UIButton(frame: emptyView.bounds)
button.setTitle("+ Add New Documentation", for: .normal)
button.titleLabel!.font = UIFont(name: "Roboto-Regular", size: 24)
button.setTitleColor(UIColor.red, for: .normal)
button.backgroundColor = UIColor.black
button.addTarget(self, action: #selector(btnTapEvent(sender:)), for: .touchUpInside)
emptyView.addSubview(button)
//more code
self.addSubview(emptyView)
}
@objc func btnTapEvent(sender:UIButton){
print("btnTapEvent")
}
}
参考图片:
推荐阅读
- c# - 如何使用控制器增加按钮单击的价值
- oracle - oracle 存储过程在另一个表的帮助下更新一个表
- java - java中条件代码的更好条件
- javascript - 异步函数挂起/恢复时是否可以透明地触发回调?
- tensorflow - 如何将新向量连接到现有的 Bert 向量中?
- usb - STM32f4 带蓝牙加密狗
- laravel - laravel ffmpeg setAdditionalParameters 使用 exportForHLS 时不添加过滤器
- mysql - 有没有办法将许多 upserts 批量发送到 mysql db?
- python - 禁用系统上的视觉效果时,TextCtrl 无法使用提示
- java - 请求和响应负载正在使用 ClientHttpRequestInterceptor 以漂亮的打印格式 JSON 打印