在程序触发的segue中传递对象?

时间:2015-09-16 10:08:29

标签: swift

我一直在阅读相当多的试图解决这个问题。你如何在一个以编程方式创建的segue中传递一个对象?我正在尝试使tableviewcell成为启动popover的原因,并且从所有读取中设置锚点的唯一方法就是如下所示。你如何以这样的方式传递一个物体?

当前代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var deckObject: AnyObject = frc.objectAtIndexPath(indexPath) as! DeckResults
    let cell: victorycellcontroller = winRecord.cellForRowAtIndexPath(indexPath) as! victorycellcontroller
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var playersNotesController: PlayerNotesViewController = storyboard.instantiateViewControllerWithIdentifier("StatWidgetPlayersNotes") as! PlayerNotesViewController
    playersNotesController.modalPresentationStyle = UIModalPresentationStyle.Popover
    let popoverplayersNoteController = playersNotesController.popoverPresentationController
    popoverplayersNoteController?.permittedArrowDirections = .Any
    popoverplayersNoteController?.delegate = self
    popoverplayersNoteController?.sourceView = cell.centerpointforpopover
    presentViewController(playersNotesController, animated: true, completion: nil)

}

1 个答案:

答案 0 :(得分:0)

让我们假设您要传递的对象是itemToPass

现在,在PlayerNotesViewController类中,创建一个对象。

var yourObject: AnyObject!

现在,更新您的代码,如下所示:

var playersNotesController: PlayerNotesViewController = storyboard.instantiateViewControllerWithIdentifier("StatWidgetPlayersNotes") as! PlayerNotesViewController
playersNotesController.yourObject = itemToPass
playersNotesController.modalPresentationStyle = UIModalPresentationStyle.Popover

您会发现itemToPassPlayerNotesViewController的值不是yourObject {/ 1}}。