После закрытия imagePickerController, как вернуться на текущую вкладку вместо вкладки по умолчанию - PullRequest
0 голосов
/ 13 февраля 2019

У меня есть 4 вкладки в панели вкладок.В настоящее время я работаю на 4-й вкладке и имею imagePickerController.После закрытия imagePickerController он переходит на вкладку по умолчанию (1-я вкладка).Как я могу сделать так, чтобы перейти на текущую (4-ю) вкладку?

class EditMyPostViewController: UIViewController {

    @IBOutlet weak var postImageView: UIImageView!


    let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker.delegate = self
    }

    @IBAction func updatePhotoTapped(_ sender: Any) {
        let alert = UIAlertController(title: "Add a picture", message: "How do you want to upload the picture?", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { action in
            self.takePhotoWithCamera()
        }))

        alert.addAction(UIAlertAction(title: "Use Existing Photo", style: .default, handler: { action in
            self.getPhotoFromLibrary()
        }))
        self.present(alert, animated: true)
    }
}

extension EditMyPostViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func takePhotoWithCamera() {
        if (!UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera)) {
            let alertController = UIAlertController(title: "No Camera", message: "The device has no camera.", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(okAction)
            present(alertController, animated: true, completion: nil)
        } else {
            imagePicker.allowsEditing = false
            imagePicker.sourceType = .camera
            present(imagePicker, animated: true, completion: nil)
        }
    }

    func getPhotoFromLibrary() {
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .photoLibrary
        present(imagePicker, animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
        {
            postImageView.image = image
        }

        dismiss(animated: true, completion: nil)
    }
}

imagePicker присутствует в 2 функциях takePhotoWithCamera() и getPhotoFromLibrary().В последней функции я попытался dismiss(animated: true, completion: nil), но вместо текущей вкладки она переходит на вкладку по умолчанию.Как я могу представить текущий ViewController?

Я пытался

present(self, animated: true, completion: nil)

Но это приведет к исключению: Приложение попыталось модально представить активный контроллер

...