当用户在详细信息视图中点击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()
}
}
答案 0 :(得分:0)
在MasterTableViewController
课程中,您需要设置DetailViewController-> favouriteDelegate to self
,例如: -
//Assuming you have reference of detailViewController inside your MasterTableViewController class
let detailViewController = DetailViewController()
detailViewController.favouriteDelegate = self
设置委托并调用方法后,应该调用它。