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

时间:2021-05-22 18:01:39

I am trying to present (1) a ScrollViewController, then after that is presented, present (2) an ImagePicker. However, only the ScrollViewController is presented, not the ImagePicker afterwards.

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

Individually, the below code works fine, just when one follows the other, it doesn't work. I've tried including one in the completion handler, still no luck.

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

There are no errors displayed, except, that when the sequence occurs, the Xcode debug area is shown but completely blank with no error messages or warnings.

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

Questions:

问题:

What exactly is going on here that I'm missing? Why aren't both being presented? How can I ensure both the ScrollViewController and ImagePicker are displayed, one after the other?

我到底究竟发生了什么?为什么不同时出现?如何确保ScrollViewController和ImagePicker一个接一个地显示?

(1) ScrollViewController:

(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:

(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 个解决方案

#1


2  

A View Controller can only present one presented controller at a time. Indeed, there is a property: presentedViewController: UIViewController? that implies this. To coordinate these actions:

视图控制器一次只能呈现一个呈现的控制器。确实,有一个属性:presentsViewController:UIViewController?这意味着这一点。协调这些行动:

A. Only present the first VC from your base VC.

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

B. In the completion handler of the first presentation call, let the second VC present the final one that goes on top:

B.在第一个表示调用的完成处理程序中,让第二个VC呈现最后一个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)
}

#1


2  

A View Controller can only present one presented controller at a time. Indeed, there is a property: presentedViewController: UIViewController? that implies this. To coordinate these actions:

视图控制器一次只能呈现一个呈现的控制器。确实,有一个属性:presentsViewController:UIViewController?这意味着这一点。协调这些行动:

A. Only present the first VC from your base VC.

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

B. In the completion handler of the first presentation call, let the second VC present the final one that goes on top:

B.在第一个表示调用的完成处理程序中,让第二个VC呈现最后一个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)
}