主视图未使用详细视图中的委托进行更新

时间:2017-07-30 17:10:31

标签: ios swift

当用户在详细信息视图中点击UISplitView时,我尝试更新UIBarButtonItem中的主视图,该视图表示收藏夹按钮。出于某种原因,委托方法永远不会被调用。相关代码如下。有什么建议吗?

import UIKit
import WebKit

protocol FavouriteCaseDelegate: class {
func updateMasterForFavouriteStatusChange(caseID: Int, favouriteStatus: Bool)
}

class DetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

weak var favouriteDelegate: FavouriteCaseDelegate? = nil

override func viewDidLoad() {
    super.viewDidLoad()

    //Configure favourite bar button
    favouriteButton = UIButton.init(type: .custom)
    if caseFavourited == false {
        favouriteButton.setImage(UIImage(named: "FavouriteIcon"), for: UIControlState.normal)
    } else {
        favouriteButton.setImage(UIImage(named: "FavouriteIconSelected"), for: UIControlState.normal)
    }
    favouriteButton.addTarget(self, action:#selector(didTapFavouriteBarButton), for: UIControlEvents.touchUpInside)
    favouriteButton.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
    let favouriteBarButton = UIBarButtonItem.init(customView: favouriteButton)
    self.navigationItem.rightBarButtonItem = favouriteBarButton
}

func didTapFavouriteBarButton() {
    caseFavourited = !caseFavourited

    CaseManager.caseWorker.changeFavouritedStateForCase(subjectID: subjectID, caseID: caseID, isFavourited: caseFavourited)

    favouriteDelegate?.updateMasterForFavouriteStatusChange(caseID: caseID, favouriteStatus: caseFavourited)
}
import UIKit

class MasterTableViewController: UITableViewController, FavouriteCaseDelegate {

func updateMasterForFavouriteStatusChange(caseID: Int, favouriteStatus: Bool) {
    print("updateMasterForFavourite called")
    self.tableView.reloadData()

    }
}

1 个答案:

答案 0 :(得分:0)

MasterTableViewController课程中,您需要设置DetailViewController-> favouriteDelegate to self,例如: -

//Assuming you have reference of detailViewController inside your MasterTableViewController class
let detailViewController =  DetailViewController()
detailViewController.favouriteDelegate = self
  

设置委托并调用方法后,应该调用它。