gpt4 book ai didi

ios - 在静态表格 View 中无法识别 Swift Touches

转载 作者:行者123 更新时间:2023-11-28 12:27:37 25 4
gpt4 key购买 nike

我有一个带有静态单元格的表格 View 。

我的第二部分有一个标题但没有行,在该部分下我有一个标签,当按下该标签时应调用 sendEmail() 函数,这将在他们的设备上打开电子邮件应用程序。

我尝试过使用标签、 TextView 、按钮,重写 didSelectCellForRow 函数,但都失败了。

我完全不知道什么东西没有被识别。

我已将打印语句添加到我的 touchesBegan 函数中,但它们从不打印。

可能是什么问题?

import UIKit
import MessageUI

class InfoTVC: UITableViewController, MFMailComposeViewControllerDelegate{

let ownerEmail = "test@email.com"
@IBOutlet weak var contactLbl: UILabel!

// MARK: - View functions

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


// MARK: - IBOutlet methods

@IBAction func backBtn(_ sender: Any) {
_ = navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}


// MARK: - Email methods

// Open users email app on device
func sendEmail(){
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients([ownerEmail])
mail.setMessageBody("<p>Hello I had the chance to use your app and </p>", isHTML: true)
present(mail ,animated: true, completion: nil)
}
else{
// Failure
print("failed to open mail")
}
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in: self.view)
if contactLbl.frame.contains(location) {
print("yes")
sendEmail()
}
else{
print("no")
}
}
}
}

最佳答案

尝试添加点击手势识别器而不是手触摸

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.youLabelTapped(_:)))
yourLabel.addGestureRecognizer(tapGesture)

或者:将目标直接添加到您的标签

yourLabel.addTarget(self, action: #selector(self.youLabelTapped(_:)), forControlEvents: .TouchUpInside)

你的函数将被调用:

func youLabelTapped(_ sender: UITapGestureRecognizer) {
print("label tapped")
}

编辑:

必需:不要忘记启用用户交互:

yourLabel.isUserInteractionEnabled = true

关于ios - 在静态表格 View 中无法识别 Swift Touches,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42913150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com