【发布时间】:2021-08-17 08:09:51
【问题描述】:
我想通过点击单元格上的菜单按钮来展示 UIAlertController
class FeedViewCell: UITableViewCell {
// some code...
lazy var menuButton: UIButton = {
let btn = UIButton()
btn.addTarget(self, action: #selector(menuTapped), for: .touchUpInside)
return btn
}()
@objc func menuTapped() {
let actionSheet = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: { action in
print("tap dismiss")
}))
actionSheet.addAction(UIAlertAction(title: "Follow", style: .default, handler: { action in
print("tap follow")
}))
present(actionSheet, animated: true)
}
}
这是一个 FeedViewController:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath) as! FeedViewCell
return cell
}
但是我收到错误“在范围内找不到‘存在’”
我不确定如何使用委托和协议来呈现它。请帮帮我
【问题讨论】:
-
只有视图控制器可以显示警报。添加一个回调闭包,将操作移动到控制器中并从那里显示警报。这是一个例子:stackoverflow.com/questions/46378515/…
-
用视图控制器来做。在 cellForRowAt 委托方法中添加一个闭包。
-
您可以在下面的链接中找到一些方法:stackoverflow.com/questions/30483104/…
标签: ios swift uitableview protocols uialertcontroller