gpt4 book ai didi

swift - 如何从不同的 View Controller 调用函数?

转载 作者:行者123 更新时间:2023-11-30 10:01:38 25 4
gpt4 key购买 nike

我有一个基本上是一个笔记应用程序,我试图从另一个 View Controller 调用一个 View Controller 中的函数。我该如何做到这一点,或者我应该有更好的方法吗?

这是我尝试调用该函数的地方

class NoteDetailViewController: UIViewController
{
@IBOutlet weak var funcButton: UIButton!

@IBAction func funcButTouched(sender: UIButton)
{
// where i want to call the function
}
}

函数在哪里

class ListTableViewController: UITableViewController
{
// the function
func wordCount()
{
var contentArr = Project.sharedInstance.content.componentsSeparatedByString(" ")
for (index, element) in contentArr.enumerate()
{
let location = conciseArr.indexOf(element)
if (location != nil)
{
contentArr[index] = inflatedArr[location!]
afterStr = contentArr.joinWithSeparator(" ")
Project.sharedInstance.after = afterStr
}
}
}
}

我尝试过创建 ListTableViewController 的实例并以这种方式调用该函数,但出现错误。

最佳答案

您可以使用delegation .

首先,声明一个委托(delegate)协议(protocol)。这应该位于您的 NoteDetailViewController.swift 文件中,但位于 class 声明之外:

protocol NoteDetailViewControllerDelegate: class {
func noteDetailViewControllerButtonTouched(controller: NoteDetailViewController)
}

接下来,将委托(delegate)属性添加到您的 NoteDetailViewController:

weak var delegate: NoteDetailViewControllerDelegate?

现在,我们使用@IBAction来告诉委托(delegate),这将是ListTableViewController:

@IBAction func funcButTouched(sender: UIButton)
{
delegate?.noteDetailViewControllerButtonTouched(self)
}

最后,回到 ListTableViewController (假设该 Controller 是在显示 NoteDetailViewController 之前的 Controller ),遵守协议(protocol)并使用 prepareForSegue > 将委托(delegate)设置为其自身:

class ListTableViewController: UITableViewController, NoteDetailViewControllerDelegate {
// ... more stuff ...

// Implement the delegate protocol
func noteDetailViewControllerButtonTouched(controller: NoteDetailViewController) {
// Do something! The button was pressed!
wordCount()
}
// Set ourselves as delegate when we are about to show the other view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let yourVC = segue.destinationViewController as? NoteDetailViewController {
yourVC.delegate = self
}
}
}
<小时/>

你应该以这样的方式结束:

NoteDetailViewController.swift:

class NoteDetailViewController: UIViewController
{
@IBOutlet weak var funcButton: UIButton!
weak var delegate: NoteDetailViewControllerDelegate?

@IBAction func funcButTouched(sender: UIButton)
{
delegate?.noteDetailViewControllerButtonTouched(self)
}
}

protocol NoteDetailViewControllerDelegate: class {
func noteDetailViewControllerButtonTouched(controller: NoteDetailViewController)
}

ListTableViewController.swift:

class ListTableViewController: UITableViewController, NoteDetailViewControllerDelegate
{
// the function
func wordCount()
{
var contentArr = Project.sharedInstance.content.componentsSeparatedByString(" ")
for (index, element) in contentArr.enumerate()
{
let location = conciseArr.indexOf(element)
if (location != nil)
{
contentArr[index] = inflatedArr[location!]
afterStr = contentArr.joinWithSeparator(" ")
Project.sharedInstance.after = afterStr
}
}
}
// Implement the delegate protocol
func noteDetailViewControllerButtonTouched(controller: NoteDetailViewController) {
// Do something! The button was pressed!
wordCount()
}
// Set ourselves as delegate when we are about to show the other view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let yourVC = segue.destinationViewController as? NoteDetailViewController {
yourVC.delegate = self
}
}
}

关于swift - 如何从不同的 View Controller 调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com