我正在尝试使用PKHUD创建自定义视图,以便在API调用失败时,我可以向用户显示一条消息以及一个呼叫帮助的按钮。
这是我的班级文件:
import UIKit
import PKHUD
class PKHUDHelpDeskView: PKHUDWideBaseView {
private func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "tel:\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
if #available(iOS 10.0, *) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
}
}
}
}
@IBAction func helpDeskNumberButton(_ sender: Any) {
callNumber(phoneNumber: "8005551234")
}
}
以下是我如何称呼它:
PKHUD.sharedHUD.contentView = PKHUDHelpDeskView()
PKHUD.sharedHUD.show()
PKHUD.sharedHUD.hide(afterDelay: 4.0)
我在storyboard中设置了一个视图(类设置为PKHUDHelpDeskView),其中包含按钮和显示消息的文本字段。当我运行它时,PKHUD显示没有文本。类文件和故事板是否正确连接,那么需要做些什么来使文本显示在PKHUD中?
答案 0 :(得分:1)
我也尝试过这样做。我没有使用storyboard添加UILabel
和UIButton
(我相信你在这里指的是.xib),而是以编程方式添加它们。以下是我的最终代码。请试一试
import PKHUD
class PKHUDHelpDeskView: PKHUDWideBaseView {
let button: UIButton = UIButton(type: UIButtonType.custom)
let label: UILabel = UILabel()
override func didMoveToSuperview() {
super.didMoveToSuperview()
button.setTitle("Call", for: UIControlState.normal)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(self.helpDeskNumberButton(_:)), for: UIControlEvents.touchUpInside)
label.text = "Call me now"
label.textColor = UIColor.brown
label.font = UIFont.systemFont(ofSize: 16)
label.textAlignment = NSTextAlignment.center
self.addSubview(label)
self.addSubview(button)
}
override func layoutSubviews() {
super.layoutSubviews()
self.button.frame = CGRect(x: 0, y: 0, width: self.frame.size.width/2, height: 30.0)
self.label.frame = CGRect(x: 0, y: 30.0, width: self.frame.size.width, height: 40.0)
}
private func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "tel:\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
if #available(iOS 10.0, *) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
}
}
}
}
func helpDeskNumberButton(_ sender: Any) {
callNumber(phoneNumber: "8005551234")
}
}