ios - Swift:我的委托(delegate)协议(protocol)方法需要在另一个 View Controller 上执行

标签 ios iphone swift delegates

这是一个非常令人困惑的问题,所以我会尽力解释它。

我有 View Controller A、B、C、D 和 E

我不确定我说的对不对,但换个说法,E 需要与 A 对话,但 A 和 E 之间没有 segue 来设置委托(delegate)。

A > B > C > D > E > A

有人告诉我,当你在 swift 中使用委托(delegate)来访问 View Controller 之间的数据时,你必须准备好一些东西:

  1. 为对象 B 定义委托(delegate)协议(protocol)。
  2. 给对象 B 一个可选的委托(delegate)变量。这个变量应该很弱。
  3. 让对象 B 在发生有趣的事情时向它的委托(delegate)发送消息 发生,例如用户按下取消或完成按钮,或者当它 需要一条信息。
  4. 使对象 A 符合委托(delegate)协议(protocol)。它应该把名字 类行中的协议(protocol)并实现协议(protocol)中的方法。
  5. 告诉对象 B,对象 A 现在是它的委托(delegate)。

我一直在使用 prepapreforsegue 在 View Controller 之间传递一个对象,因为它们从一个导航到下一个,并在它们继续通过 prepareforsegue 方法时添加到它。

在最后一个 View Controller 上,我有协议(protocol)方法,这些方法是要在第一个 View Controller 上执行的,但是因为最后一个 View Controller 从另一个 View Controller 中分离出来。问题是它应该在第一个 View Controller 上执行这些方法。如果第一个 View Controller 无法告诉最后一个 View Controller 它是 segue,它如何到达它。

这里有一些简单的代码可以向您展示我的问题和我的意思。

首先:

import UIKit

class FirstViewController: UITableViewController, LastViewControllerDelegate {

var entry: EntryItem!

override func viewDidLoad() {
    super.viewDidLoad()
    entry = EntryItem()
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    //here's where I access the next view controller and set controller.entry and controller.delegate
    let controller = segue.destinationViewController as NextViewController
    controller.entry = entry
}

func lastViewControllerDidCancel(controller: LastViewController, didNotFinishAddingEntry draftentry: EntryItem){
    //this is where I wanna do something
    dismissViewControllerAnimated(true, completion: nil)
}
func lastViewController(controller: LastViewController, didFinishAddingEntry finalentry: EntryItem){
    //this is where I wanna do something
    dismissViewControllerAnimated(true, completion: nil)
}
}

下一个:

import UIKit


class Next1ViewController: UITableViewController, LastViewControllerDelegate {

var entry: EntryItem!

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    //here's where I access the next view controller and set controller.entry and controller.delegate
    let controller = segue.destinationViewController as Next2ViewController
    controller.entry = entry
}


}

下一个 2:

import UIKit


class Next2ViewController: UITableViewController, LastViewControllerDelegate {

    var entry: EntryItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    ...

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        //here's where I access the next view controller and set controller.entry and controller.delegate
        let controller = segue.destinationViewController as LastViewController
        controller.entry = entry
        controller.delegate = ???? //Not self, but needs to be the first one.
    }


}

最后:

import UIKit

protocol LastViewControllerDelegate: class {
    func lastViewControllerDidCancel(controller: LastViewController, didNotFinishAddingEntry draftentry: EntryItem)
    func lastViewController(controller: LastViewController, didFinishAddingEntry finalentry: EntryItem)
}

class LastViewController: UITableViewController {

weak var delegate: LastViewControllerDelegate?
var entry: EntryItem!

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

...


@IBAction func cancel(){
    delegate?.lastViewControllerDidCancel(self, didNotFinishAddingEntry: entry)
}

@IBAction func done(){
    //do some final additions to entry object
    delegate?.lastViewController(self, didFinishAddingEntry: entry)
}

}

最佳答案

难道你不能让你的第一个 View Controller 符合所有的委托(delegate)协议(protocol)(或者,可能,做一个单一的委托(delegate)协议(protocol),否则你会遇到转换问题)然后而不是这样做:

controller.delegate = self

就这样

controller.delegate = self.delegate

或者,如果您已经删除了几个步骤,使用通知通常比委派更好。如果介入的 View Controller 除了转发它之外对委托(delegate)没有任何目的,你应该使用通知。

关于ios - Swift:我的委托(delegate)协议(protocol)方法需要在另一个 View Controller 上执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27670734/

相关文章:

ios - Div 向下移动到页面底部而不是 iOS 中屏幕高度的按钮

iphone - 使用 SVGKit 加载 SVG 文件

ios - Firebase:检索为对象/模型。来自 java

ios - 如何使用 Swift 创建具有动态单元格高度的静态单元格

ios - 如何在swift3中datePicker索引 Realm 并绘制图表

ios - 如何在 iOS 8 中为特定 Controller 禁用自动旋转?

iphone - 如何开发自动错过的调用回复Messenger iOS移动应用程序

ios - 自调整 UITableViewCell 大小

ios - 警告 : Format specifies type 'long' but the argument has type 'UIWebViewNavigationType' ( aka 'enum UIWebViewNavigationType' )

ios - 内部 iOS 应用程序有哪些分发选项?