通过segue发送的任何内容都返回nil

时间:2015-08-19 00:15:32

标签: ios swift segue

下面是我的segue代码,但是当我尝试在另一个视图控制器中访问它们时,所有变量都返回nil。我甚至添加了一个简单的字符串值来发送,并返回nil。

extension ViewController : MPMediaPickerControllerDelegate {
// must implement these, as there is no automatic dismissal

func mediaPicker(mediaPicker: MPMediaPickerController!, didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {

    let ViewControllerhost:ViewControllerHost = storyboard?.instantiateViewControllerWithIdentifier("HostViewController") as! ViewControllerHost

    let representativeItem = mediaItemCollection.representativeItem
    let title = representativeItem.title
    let artistName = representativeItem.artist

    segueInfo =
        [ "representativeItem": representativeItem
            , "title": title
            , "artistName": artistName ]

    var albumCover  = "\(MPMediaItemArtwork())"
    var songInfo = "\(MPMediaItem())"
    var messageDict = [albumCover, songInfo]

    //This sends across Album Artwork, Info, and the duration is sent from the Host view controller
    let messageData = NSJSONSerialization.dataWithJSONObject(messageDict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)

    var error:NSError?

    appDelegate.mpcHandler.session.sendData(messageData, toPeers: appDelegate.mpcHandler.session.connectedPeers, withMode: MCSessionSendDataMode.Unreliable, error: &error)

    if error != nil{
        println("error: \(error?.localizedDescription)")
    }

     func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if let nextVC = segue.destinationViewController as? ViewControllerHost
            , let sender = sender as? ViewController {
                /*nextVC.representativeItem_ = sender.segueInfo["representativeItem"] as? MPMediaItem
                nextVC.title_ = sender.segueInfo["title"] as! String
                nextVC.artistName_ = sender.segueInfo["artistName"] as! String*/
                nextVC.representativeItem_ = representativeItem
                nextVC.title_ = title
                nextVC.artistName_ = artistName
                nextVC.test_ = test
        }
    }

    dismissViewControllerAnimated(true, completion: {
        self.navigationController?.pushViewController(ViewControllerhost, animated: true)
    })
}








func mediaPickerDidCancel(mediaPicker: MPMediaPickerController!) {
    self.dismissViewControllerAnimated(true, completion: nil)

}

}

3 个答案:

答案 0 :(得分:1)

我怀疑这个条件是失败的:

if let nextVC = segue.destinationViewController as? ViewControllerHost, let sender = sender as? ViewController {

特别是if let sender = sender as? ViewController {部分。

根据docssender是触发segue的按钮或视图。它不是视图控制器本身,因此if let sender = sender as? ViewController {可能返回false。

答案 1 :(得分:0)

不是100%确定这是否有帮助,但这是我使用的

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showTaskDetail" {
        let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
        let indexPath = self.tableView.indexPathForSelectedRow()
        let thisTask = fetchedResultsController.objectAtIndexPath(indexPath!) as! TaskModel
        detailVC.detailTaskModel = thisTask
        detailVC.delegate = self

    }
    else if segue.identifier == "showTaskAdd" {
        let addTaskVC:AddTaskViewController = segue.destinationViewController as! AddTaskViewController
        addTaskVC.delegate = self
    }
}

当你继续使用故事板时,点击segues并在属性下给他们一个标识符

也许尝试这种方法?如果它不起作用,请告诉我

答案 2 :(得分:0)

我想你错过了这个:         self.performSegueWithIdentifier(segweId,sender:self)