ios - 如何检测弹出窗口中的点击事件? ( swift )

标签 ios objective-c swift

我在我的 MainViewController 中使用 UITableViewController 作为弹出窗口。我想要实现的是检测 tableview 点击并在我的 MainViewController 中执行一些操作。我该如何实现?

这就是我创建弹出窗口的方式:

let count = sender.dropdownItems?.count

let storyboard : UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var menuViewController: MoreTableViewController = storyboard.instantiateViewControllerWithIdentifier("moreTable") as! MoreTableViewController
menuViewController.data = sender.dropdownItems!

menuViewController.modalPresentationStyle = .Popover
menuViewController.preferredContentSize = CGSizeMake(300, 44*CGFloat(count!))

let popoverMenuViewController = menuViewController.popoverPresentationController
popoverMenuViewController?.permittedArrowDirections = .Any
popoverMenuViewController?.delegate = self
popoverMenuViewController?.sourceView = sender
presentViewController(menuViewController,animated: true,completion: nil)

这是 MoreTableViewController:

import UIKit

class MoreTableViewController: UITableViewController {

    //var data: [MoreItem]?

    var data = [MoreItem]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.scrollEnabled = false

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }



    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return data.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("moreCell", forIndexPath: indexPath) as! MoreTableViewCell

        cell.label.font = UIFont.fontAwesomeOfSize(17)
        cell.label.textColor = UIColor.init(hexString: MyConstants.COLOR_GREY)
        cell.label.text = data[indexPath.row].label


        return cell
    }


    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("Do something in MainViewController")
    }



}

最佳答案

厨师,

您可以使用委托(delegate)。

在您的 MoreTableViewController 中将协议(protocol)添加为

protocol LetsInformPreviousViewControllerProtocol : class {
    func cellTapped(WithIndex index : NSIndexPath) //pass whatever the data u want to pass to previous View controller here am passing indexPath of selected cell
}

并创建一个弱引用变量来保存委托(delegate)类的引用。

weak var delegate : LetsInformPreviousViewControllerProtocol?

最后在你的 didSelectRowAtIndexPath 中

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.delegate?.cellTapped(WithIndex: indexPath) 
    }

并在您的 MainViewController 中确认协议(protocol) LetsInformPreviousViewControllerProtocol 使用

class MainViewController: UIViewController, LetsInformPreviousViewControllerProtocol

当你呈现 MoreTableViewController 时说

let storyboard : UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var menuViewController: MoreTableViewController = storyboard.instantiateViewControllerWithIdentifier("moreTable") as! MoreTableViewController
menuViewController.delegate = self

最后一步实现方法 cellTapped as

func cellTapped(WithIndex index: NSIndexPath) {
        print("\(index)")
    }

这将使您的代码避免崩溃且健壮。

避免持有对 ViewController 的强引用,这可能会导致内存泄漏。

关于ios - 如何检测弹出窗口中的点击事件? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37743141/

相关文章:

ios - NSTimer 每 30 秒运行一次,但总是在 xx :00sec or xx:30sec of a minute 上触发

ios - UICollectionView 单元格中的 UIActivityIndi​​cator 随机不会隐藏

ios - 传递对象时无法分配类型的值

objective-c - OCLint 在 html 报告文件中出现编译错误,但我的项目构建成功。为什么

ios - 在 swift 数据模型中使用来自 objective-c++ 框架的数据类型

ios - 如何使用视觉格式语言添加 View 的纵横比?

ios - 为什么我的 UIDatePicker 在 iOS9 中消失了?

objective-c - 如何从 JSON 中解析 base64 图像

json - 从 documentDirectory 在本地编码/解码 JSON

ios - 在 swift 中使用 tableView 自动填充委托(delegate)方法的最佳方法?