委托传递给其他ViewController

时间:2016-08-28 21:56:08

标签: swift core-data uiviewcontroller delegates

我想将来自HeaterViewController的var delegate: Diary?委托传递给我的DetailViewController,以获取它的核心数据属性。所以我在func didselectItem view.delegate = diarys中设置了委托,但它没有运行我没有segue在视图控制器之间传递数据。所以问题是如何在视图控制器之间传递委托以从核心数据条目中获取标题。 另一件事是我想从另一个viewcontroller访问headerviewcontroller中的func。我试过

let vc = storyboard!.instantiateViewController(withIdentifier: "DiaryController") as! DiaryController
    vc.loadDiarys()

但它没有运行所以我需要来自此viewcontroller中的DiaryController(loadDiarys)的func。我没有使用它。

DetailViewController的代码:

var delegate: Diary?

AddTitle.text = delegate.title

来自HeaderViewController的代码:

var diary = [Diary] () {
    didSet {
        self.DiaryCollectionView.reloadData()
    }
}
var mgdContext = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext

加载新条目:

func loadDiarys () {
    let loadRequest: NSFetchRequest<Diary> = Diary.fetchRequest()
    diary = try! mgdContext.fetch(loadRequest as!     NSFetchRequest<NSFetchRequestResult>) as! [Diary]
    print("loadD")
}

将委托移交给其他ViewController(DetailDiaryController)

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let diarys = diary[indexPath.row]

        let view = DetailDiaryController.instantiateFromNib()

        view.delegate = diarys





        let window = UIApplication.shared.delegate?.window!
        let modal = PathDynamicModalPan()
        modal.showMagnitude = 200.0
        modal.closeMagnitude = 130.0
        view.closeButtonHandler = {[weak modal] in
            modal?.closeWithLeansRandom()
            return
        }
        view.bottomButtonHandler = {[weak modal] in
            modal?.closeWithLeansRandom()
            return
        }
        modal.show(modalView: view, inView: window!)

    }
}

0 个答案:

没有答案