这是我的第一个问题,希望不会太蠢。
这是我的应用程序:
VC1
VC2 - ChatTableViewController
VC3 - PopupPhotoSourceVC
VC1 -一个用于显示VC2的按钮
VC2 -一个表示VC3的“条形”按钮(VC3表示设置为“超过当前上下文”)
VC2 -设置一个展开器
@IBAction func unwindToChatTableViewController(segue: UIStoryboardSegue) {
attachPhotoButtonFinish()
}
VC3 -中间有两个按钮,通过拖动和使用StoryBoard进行选择,都被设置为高于解风的位置。
在VC3内部,使用备战让VC2知道用户选择了什么。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destChatViewController = segue.destination as! ChatTableViewController
if segue.identifier == "library" // library Button
{
print("set cameraType to library from popupviewcontroller")
let time = getCurrentTime()
print("==== timer start ====== \(time.3):\(time.4):\(time.5)")
destChatViewController.cameraType = "library"
}
else if segue.identifier == "newphoto" // newphoto Button
{
print("set cameraType to newphoto from popupviewcontroller")
let time = getCurrentTime()
print("==== timer start ====== \(time.3):\(time.4):\(time.5)")
destChatViewController.cameraType = "newphoto"
}
// for unwind segue, no need to call dismiss
print("not calling dismiss pop up")
let time = getCurrentTime()
print("==== timer start ====== \(time.3):\(time.4):\(time.5)")
//dismiss(animated: true, completion: nil)
}
在VC2内部,attachPhotoButtonFinish将尝试根据用户的选择调用图像选择器控制器
func attachPhotoButtonFinish() {
print("attachphotobutton start")
let time = getCurrentTime()
print("==== timer start ====== \(time.3):\(time.4):\(time.5)")
let image = UIImagePickerController()
image.delegate = self
if cameraType == "library"{
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
}
else if cameraType == "newphoto"{
image.sourceType = UIImagePickerControllerSourceType.camera
image.cameraCaptureMode = .photo
}
else{
print("Something wrong!!!")
return
}
image.allowsEditing = false
image.modalPresentationStyle = .overCurrentContext
print("calling imagepicker")
let time1 = getCurrentTime()
print("==== timer start ====== \(time1.3):\(time1.4):\(time1.5)")
self.present(image, animated: true, completion: nil)
}
我还试着打印VC3的时间
deinit{
print("===== \(self.classForCoder.description()) be deinit")
let time = getCurrentTime()
print("==== timer start ====== \(time.3):\(time.4):\(time.5)")
}
关于运行结果的,有时(1 / 10) VC3在VC2显示图像选择器之前完成。这样我就可以正确地看到它的显示。
====计时器启动====== 18:34:58
不叫解散弹出
====计时器启动====== 18:34:58
解压缩到ChatTableViewController cameraType =库
====计时器启动====== 18:34:58
拍光按钮启动
====计时器启动====== 18:34:58
呼叫成像仪
====计时器启动====== 18:34:58
===== PhotoStreamSourceTest.PopupPhotoSourceVC be deinit
====计时器启动====== 18:34:59
18:34:59.274464+0800 PhotoStreamSourceTest14690:399530请求取消PID:14706名称暂停的客户端:
2017-09-20 18:34:59.283271+0800 PhotoStreamSourceTest14690:399463 systemgroup.com.apple.configurationprofiles路径系统组容器为systemgroup.com.apple.configurationprofiles
18:34:59.284012+0800 PhotoStreamSourceTest14690:399463阅读私人有效用户设置。
,但大多数情况下,我会收到以下结果,并且图像选择器不会显示.
====计时器启动====== 18:35:48
不叫解散弹出
====计时器启动====== 18:35:48
解压缩到ChatTableViewController cameraType =库
====计时器启动====== 18:35:48
拍光按钮启动
====计时器启动====== 18:35:48
呼叫成像仪
====计时器启动====== 18:35:48
18:35:48.967515+0800 PhotoStreamSourceTest14690:400836请求取消PID:14706名称暂停的客户端:
2017-09-20 18:35:49.049357+0800 PhotoStreamSourceTest14690:399463警告:在演示文稿进行时尝试呈现!
===== PhotoStreamSourceTest.PopupPhotoSourceVC be deinit
====计时器启动====== 18:35:49
谢谢!
发布于 2017-09-20 13:26:02
感谢@chirag90 90的链接Meaning of Warning “while a presentation is in progress!。
关键是在让启动图像选择器之前,使用方法的完成块来确保VC3是真正关闭的。
下面的解决方案使用委托而不是展开segue。如果有人有其他方法,请告诉我!谢谢!
首先,在VC3上,从Storyboard中删除两个按钮上的展开子。然后在名为libraryButtonPressed()和takePhotoButtonPressed()的两个按钮上添加takePhotoButtonPressed()
在VC3:PopupPhotoSourceVC上添加一个协议
protocol PopupPhotoSourceVCDelegate {
func finishPassing(string: String)}
在VC3上还添加了该类型的委托变量。
var delegate: PopupPhotoSourceVCDelegate?
现在在VC2上,采用协议
class ChatTableViewController: UITableViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, PopupPhotoSourceVCDelegate{
并实现了服从协议的finishPassing函数。
func finishPassing(string: String) {
cameraType = string
attachPhotoButtonFinish()
}
别忘了把委托给自己
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PopupPhotoSourceVC{
destination.delegate = self
}
}
现在,回到VC3,让我们实现关键方法
@IBAction func libraryButtonPressed(_ sender: UIButton) {
dismiss(animated: true) {
self.delegate?.finishPassing(string: "library")
}
}
@IBAction func takePhotoButtonPressed(_ sender: UIButton) {
dismiss(animated: true) {
self.delegate?.finishPassing(string: "newphoto")
}
}
这两种方法将排除VC3:PopupPhotoSourceVC,在解散之后实际上已经完成,然后调用VC2中的finishPassing :ChatTableViewController启动图像选择器。
希望能帮上忙谢谢。
https://stackoverflow.com/questions/46320067
复制相似问题