gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:23 24 4
gpt4 key购买 nike

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

我有 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/

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