如何解决从单元格呈现 UIAlertController
我想通过点击单元格上的菜单按钮来呈现 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
}
但是我收到错误“在范围内找不到'存在'”
我不确定如何使用委托和协议来呈现它。请帮帮我
解决方法
Ivan 为 UIViewController 寻找响应者链的方法可行,但我建议采用不同的方法。
修改您的 FeedViewCell
类以获得可选的 viewController 属性:
class FeedViewCell: UITableViewCell {
// Note that the pointer to the view controller should be weak
// To avoid a retain cycle,as pointed out by Matt.
weak var viewController: UIViewController? = nil
// some code...
}
并修改您的 IBAction 方法:
@IBAction func menuTapped() {
// Make sure `viewController` is not nil.
guard let viewController = viewController else {
print("No view controller found. Exiting.")
return
}
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")
}))
// Change your code to send the `present(_:animated:)` message to
// the cell's `viewController`.
viewController.present(actionSheet,animated: true)
}
}
然后在配置 FeedViewCell 单元格的视图控制器中,设置单元格的 viewController 属性:
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Replace with the correct reuse identifier
guard let cell = tableView.dequeueReusableCell(withIdentifier: yourIdentifier) as? FeedViewCell else {
return UITableViewCell()
}
cell.viewController = self // Or,if the data source is not the main view controller for the window,pass in some other view controller.
}
,
对于单元格内的当前警报,您应该获得父控制器。警报控制器只能存在于其他父控制器上。
让我们做吧。首先,添加扩展。它允许获取使用以下视图的控制器:
public extension UIView {
var viewController: UIViewController? {
weak var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
现在您可以在单元格中获取控制器。让我们做吧:
@objc func menuTapped() {
// You added alert here,your code
guard let parentController = self.viewController else { return }
parentController.present(actionSheet,animated: true)
}
就这些。
,你的整个方法是错误的;控制周围的视图控制器与单元格无关。但是,如果您坚持,请使用此实用程序:
extension UIResponder {
func next<T:UIResponder>(ofType: T.Type) -> T? {
let r = self.next
if let r = r as? T ?? r?.next(ofType: T.self) {
return r
} else {
return nil
}
}
}
现在
self.next(ofType: FeedViewController.self)?.present...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。