簡體   English   中英

從父級重新加載TableViewController

[英]Reload TableViewController from parent

我有一個TableViewController,讓我們稱之為A ,它在另一個視圖控制器B的容器視圖中。 B中的值更改時,我需要A重新加載其數據。 我還需要它來從B獲得此更改的值。 有任何想法嗎?

您是否考慮過使用通知

因此,在B中 -我會做類似的事情:

// ViewControllerB.swift

import UIKit

static let BChangedNotification = "ViewControllerBChanged"

class ViewControllerB: UIViewController {

    //... truncated

    func valueChanged(sender: AnyObject) {
        let changedValue = ...
        NSNotificationCenter.defaultCenter().postNotificationName(
            BChangedNotification, object: changedValue)
    }

    //... truncated
}

接下來是A,看起來像這樣-其中ValueType只是您提到的的類型:

import UIKit

class ViewControllerA: UITableViewController {

    //... truncated

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        //...truncated

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "onBChangedNotification:",
            name: BChangedNotification,
            object: nil)
    }

    //... truncated

    func onBChangedNotification(notification: NSNotification) {
        if let newValue = notification.object as? ValueType {

            //...truncated (do something with newValue)

            self.reloadData()
        }
    }
}

最后–不要忘記在Adeinit方法中刪除觀察者

import UIKit

class ViewControllerA: UITableViewController {

    //... truncated 

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    //... truncated
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM