簡體   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