繁体   English   中英

Swift:我的代理协议方法需要在另一个视图控制器上执行

[英]Swift: My Delegate Protocol Methods Need to Execute on Another View Controller

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

我有视图控制器A,B,C,D和E.

我不确定我是否说得对,但重新说明一下,E需要与A交谈,但A和E之间没有设置委托的区别。

A> B> C> D> E> A.

有人告诉我,当你在swift中使用代理来访问视图控制器之间的数据时,你必须有很多东西:

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

我一直在使用prepapreforsegue在视图控制器之间传递一个对象,因为它们从一个到另一个导航,在它们继续通过prepareforsegue方法时添加它。

在最终视图控制器上,我有协议方法,这些方法意味着要执行第一个视图控制器,但因为最后一个视图控制器从不同的视图控制器中分离。 问题是它应该在第一个视图控制器上执行这些方法。 如果没有第一个视图控制器能够告诉最后一个视图控制器它是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)
}
}

下一个1:

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

}

你不能让你的第一个视图控制器符合所有委托协议(或者,可能,制作一个委托协议,否则你会得到转换问题),然后不要这样做:

controller.delegate = self

做就是了

controller.delegate = self.delegate

或者,如果您已经删除了几个步骤,则使用通知通常比委派更好。 如果介入视图控制器除了转发它之外没有委托的目的,您应该使用通知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM