如何从其他视图控制器调用函数?

时间:2016-07-05 05:40:46

标签: swift string function uiviewcontroller viewcontroller

我的基本上是一个笔记应用程序,我试图调用另一个视图控制器中的一个函数。我该怎么做,还是有更好的方法呢?

这是我试图从

调用该函数的地方
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实例,只是以这种方式调用函数,但是我收到错误。

2 个答案:

答案 0 :(得分:1)

您可以使用delegation

首先,声明一个委托协议。这应该在NoteDetailViewController.swift文件中但在class声明之外:

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

接下来,向您的NoteDetailViewController添加委托属性:

weak var delegate: NoteDetailViewControllerDelegate?

现在,我们使用@IBAction告诉代表,这将是ListTableViewController

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

最后,回到ListTableViewController(假设此控制器是显示NoteDetailViewController之前的控制器),遵循协议并使用prepareForSegue将委托设置为自身:< / p>

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
        }
    }
}

答案 1 :(得分:0)

只需按照下面的代码进行操作即可: -

NoteDetailViewController.swift:

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

    var parentListTableView : ListTableViewController!
    @IBAction func funcButTouched(sender: UIButton)
    {
        parentListTableView.wordCount()

    }
}

ListTableViewController.swift:

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
            }
        }
    }

    // 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.parentListTableView = self
        }
    }
}