下面是我在下面的segue的代碼,但是當我嘗試在另一個視圖控制器中訪問它們時,所有變量返回nil。我甚至添加了一個簡單的字符串值發送,並返回零。通過segue發送的任何內容返回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)
}
}
你確定你的'if let nextVC = segue.destinationViewController as? ViewControllerHost,讓發件人=發件人爲? ViewController {'條件正在執行?我必須檢查,但'發送者'可能不是類型的ViewController ... –
我同意@LyndseyScott - 你有兩個可選的綁定那裏,可能會失敗。你是否設置了一個斷點來確認if中的代碼執行? – Paulw11