繁体   English   中英

从另一个视图调用UITableViewFunction

[英]Call a UITableViewFunction from another view

我有一个由UITableView调用的function ,该function根据其中包含的内容更新钱包值(因此,通过tableView进行调用)。 这是WalletViewController一部分,用户可以在其中更新其钱包中包含的内容。

我希望能够从MainViewController调用此函数,在该函数中还显示了钱包值,因此当用户刷新数据时它是最新的,现在钱包值更新功能仅在加载WalletViewController时发生然后调用reloadData()

我应该怎么做才能从MainViewController刷新值?

用于值更新功能的WalletViewController代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell

    cell.delegate = self
    cell.amountTextField.delegate = self

    updateCellValueLabel(cell, atRow: indexPath.row)

    return cell
}

func updateCellValueLabel(_ walletTableViewCell: WalletTableViewCell, atRow row: Int) {

    //... calculations for each wallet's object are happening here ...

    CoreDataHandler.editObject(editObject: selectedAmount, amount: amount, amountValue: currentAmountValue) 
    // <--- calculations result for each object is saved to CoreData

    updateWalletValue()
    updateWalletLabel()
}

func updateWalletValue() {

    var items : [CryptosMO] = []

    if CoreDataHandler.fetchObject() != nil {
        items = CoreDataHandler.fetchObject()! 
    // <--- Calculations result are fetched from CoreData
    }

    total = items.reduce(0.0, { $0 + Double($1.amountValue)! } )
    // <--- Objects values are added together to get the wallet's grand total

    WalletTableViewController.staticTotal = total
}

func updateWalletLabel() {

    walletValueLabel.text = formatter.string(from: NSNumber(value: total))

}

到目前为止,MainViewController钱包更新功能:

func updateWalletLabel() {

    WalletTableViewController.init().updateWalletValue()

    walletValue.text = formatter.string(from: NSNumber(value: WalletTableViewController.staticTotal))

}

我想我应该从核心数据中获取钱包对象的金额,并从MainViewController重做计算以获取最新值? 还是有一个更简单的解决方案来调用整个tableView函数?

您可以通过几种方法来做到这一点。 如果您想走“发布通知”路线,@ MRizwan33的答案将起作用。

我通常要做的另一种选择是从MainViewController获取对WalletViewController的引用,然后直接在WalletViewControllerInstance上进行WalletViewControllerInstance

class MainViewController: UIViewController {

    // Local variable to cache instance of WalletViewController
    var walletVC: WalletViewController?

    func functionWhereWalletViewControllerIsInstantiated() {
        // Save the reference to the WalletViewController instance
        walletVC = self.storyboard?.instantiateViewController(
                withIdentifier: "WalletViewControllerIdentifier") as!
                        WalletViewController
    }

    func someFuncWhereYouWantToUpdateWalletViewController() {
        // Call the function on your cached WalletViewController
        // instance that performs the refresh
        walletVC.doTheUpdate()
    } 
}

如果在将其推walletVC地方实例化walletVC ,则可以执行以下操作:

func functionWhereWalletVcIsPushed() {
    if (walletVC == null) {
        walletVC = self.storyboard?.instantiateViewController(
                withIdentifier: "WalletViewControllerIdentifier") as!
                        WalletViewController
    }
    // Push the view controller
}

您还可以使属性变得懒惰:

lazy var walletVC: WalletViewController = {
    return self.storyboard?.instantiateViewController(
                    withIdentifier: "WalletViewControllerIdentifier") as!
                            WalletViewController
}

(我不是Swift专家,所以您需要仔细检查语法)

在您要调用的位置使用此行来更新另一个视图。

NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)

添加将在其中发生更新的这些行:

NotificationCenter.default.addObserver(self, selector: #selector("Your Method Name Which you want to call on change like (reloadTableView)"), name: Notification.Name("NotificationIdentifier"), object: nil)

暂无
暂无

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

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