我有这段代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//dismiss(animated: true, completion: nil)
guard info[UIImagePickerControllerMediaType] != nil else { return }
let mediaType = info[UIImagePickerControllerMediaType] as! CFString
print("video is",mediaType)
switch mediaType {
case kUTTypeImage:
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage, let pickedAsset = info[UIImagePickerControllerPHAsset] as? PHAsset {
self.screenshotOut = pickedImage
MyVariables.isScreenshot = true
self.thumbnailImage = pickedImage
let creationDate = pickedAsset.creationDate
self.mediaDate = creationDate
dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
}
break
....
default:
dismiss(animated: true, completion: nil)
print("something else")
break
}
}
你可以看到它等到我的imagePickerController有一个照片资产。然后它设置一些类变量并执行segue。我有这个功能来准备segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "CreatePost_Segue" {
guard self.thumbnailImage != nil else {
return
}
if MyVariables.isScreenshot == true {
guard self.screenshotOut != nil else {
return
}
//self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
} else {
guard self.thumbnailImage != nil else {
return
}
//self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
//now I set those three varibles?
}
let controller = segue.destination as! CreatePostViewController
controller.postDate = mediaDate
controller.thumbnailImage = thumbnailImage
controller.videoURL = videoURL
controller.screenshotOut = screenshotOut
print("segued",controller.postDate)
print("segued",controller.thumbnailImage)
print("segued",controller.videoURL)
print("segued",controller.screenshotOut)
}
}
打印:
segued Optional(2016-05-04 17:48:53 +0000)
segued Optional(<UIImage: 0x13d365720> size {2448, 3264} orientation 0 scale 1.000000)
segued nil
segued Optional(<UIImage: 0x13d365720> size {2448, 3264} orientation 0 scale 1.000000)
所以看起来所有的控制器分配都有效...... videoURL故意为零而且是可选的,所以我相信没关系。
但是segue永远不会发生。拾取器控制器被解雇了,这很好,但为什么这不是一个不错的选择呢?
答案 0 :(得分:1)
因为dismiss是动画的,VC目前正在展示相机/照片libaray所以它不能呈现另一个,你可以发送它
dismiss(animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
或以获得正确的延迟
self.dismiss(animated: true) {
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
//您可能会在控制台
中查找VC当前正在显示的消息