从UITableViewCell调用视图控制器中的函数 [英] Call function in view controller from UITableViewCell

查看:106
本文介绍了从UITableViewCell调用视图控制器中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个让我疯狂的问题,如何在UITableViewCell中调用视图控制器中的函数。 - 我在Swift中这样做。

I have a problem which drives me nuts, how is it possible to invoke a function in a view controller from within a UITableViewCell. - I am doing that in Swift.

所以我有一个View Controller - 在这个mainVC中叫做mainVC
我已经声明了一个包含UITableViewCell的tableView(Custom / dynamic)在那个tableView单元格中我想调用一个在mainVC中声明的函数。如果我喜欢self.superview?.myFunc不起作用。

So I have a View Controller - called mainVC in this mainVC I have declared a tableView which holds a UITableViewCell (Custom/ Dynamic) in that tableView cell I want call a function which is declared in the mainVC. If I do like self.superview?.myFunc doesn't work.

我希望有人可以帮助我...

I hope someone can help me...

推荐答案

它不幸的是,并不那么容易。
您想要了解的是NSNotificationCenter甚至更好: - 代表。
如果你想更好地理解代表我会推荐这个教程: http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views

It is not as easy as that unfortunately. What you want to look into is NSNotificationCenter or even better: - Delegates. If you want a better understanding of Delegates I'd recommend this tutorial: http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views

如果您不想阅读所有内容,我会尽力在此解释。

If you don't want to read all that, I'll try my best to explain it here.

假设您有两个view controllers:mainVC和otherVC。

Let's say you have two view controllers: mainVC and otherVC.

在otherVC的类定义之上,添加以下代码:

Above the class definition in otherVC, add the following code:

protocol OtherVCDelegate {
   func cellClicked()
}

在otherVC的类中,添加以下属性:

Inside the class in otherVC, add the following property:

var delegate: OtherVCDelegate?

在didSelectRow(或执行代码的地方)中,添加以下代码:

In the didSelectRow (or where you execute the code), add the following code:

self.delegate?.cellClicked()

现在,回到mainVC:通过将mainVC添加到类中,使mainVC符合此委托:

Now, back in mainVC: Make mainVC conform to this delegate by adding it to the class like this:

class MainViewController: UIViewController, DetailDelegate {
}

在MainViewController中添加委托函数和确保你想要执行你的函数:

In MainViewController add the delegate function and insite that one put your function you want to execute:

func cellClicked() {
    println("Cell was clicked")
    myFunc()
}

你要做的最后一件事是使MainViewController成为OtherViewController的委托。必须在从mainVC访问otherVC的地方完成此操作。假设它是在prepareForSegue中完成的:

The last thing you have to do is make MainViewController the delegate of OtherViewController. This has to be done where you access the otherVC from the mainVC. Let's say it is done in prepareForSegue:

if segue.identifier == "showDetail" {
       (segue.destinationViewController as DetailViewController).delegate = self
    }

起初有点复杂,但是一旦你掌握了它就在公园散步。

It is a little bit complicated at first, but once you get a grasp of it it's a walk in the park.

这篇关于从UITableViewCell调用视图控制器中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆