从UITableViewCell调用视图控制器中的函数

编程入门 行业动态 更新时间:2024-10-09 05:25:32
本文介绍了从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甚至更好: - 代表。 如果你想更好地理解代表我会推荐这个教程: www.raywenderlich/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: www.raywenderlich/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调用视图控制器中的函数

本文发布于:2023-11-17 11:03:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   函数   器中   UITableViewCell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!