在一个函数中以编程方式呈现两个UIViewControllers

时间:2016-08-26 22:24:50

标签: ios swift uiviewcontroller uiimagepickercontroller presentviewcontroller

我试图呈现(1)一个ScrollViewController,然后在呈现之后,呈现(2)一个ImagePicker。但是,仅显示ScrollViewController,而不是之后的ImagePicker

单独地,下面的代码工作正常,只要一个跟随另一个,它就不起作用。我试过在完成处理程序中包含一个,但仍然没有运气。

没有显示错误,除了,当序列发生时,Xcode调试区域显示但完全空白,没有错误消息或警告。

问题:

  

我到底发生了什么,我错过了什么?为什么都没有出席?如何确保ScrollViewControllerImagePicker一个接一个地显示?

(1)ScrollViewController:

 let storyboard = UIStoryboard(name: "Main", bundle: nil)
 let vc = storyboard.instantiateViewControllerWithIdentifier("ScrollViewControllerID") as! ScrollViewController
 self.presentViewController(vc, animated: true, completion: nil)

(2)ImagePicker:

 let imagePicker = MyImagePickerController()
 imagePicker.delegate = self
 imagePicker.sourceType = .PhotoLibrary
 imagePicker.allowsEditing = false
 imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
 self.presentViewController(imagePicker, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:3)

View Controller一次只能呈现一个呈现的控制器。实际上,有一个属性:presentedViewController: UIViewController?暗示了这一点。协调这些行动:

一个。仅显示基础VC中的第一个 VC。

B中。在第一个演示文稿调用的完成处理程序中,让第二个 VC呈现最后的那个:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ScrollViewControllerID") as! ScrollViewController
self.presentViewController(vc, animated: true) {
    let imagePicker = MyImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .PhotoLibrary
    imagePicker.allowsEditing = false
    imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
    vc.presentViewController(imagePicker, animated: true, completion: nil)
}