Minimoore26
Minimoore26

Reputation: 17

View Controller won't dismiss and present new view controller immediately

I'm trying to present a view controller once the QRCode reader has been dismissed, however when doing this the QRCode reader view controller is presented again. The code snippet below shows the method and how I'm dismissing the view and how I'm trying to present the next view controller. Any idea on why the QR reader view controller keeps presenting its self when I try to present a different controller.

func readerDidCancel(_ reader: QRCodeReaderViewController) {
    dismiss(animated: true, completion: nil)
    present(ClockInOrOutViewController(), animated: true, completion: nil)
}

Upvotes: 1

Views: 1524

Answers (2)

Rakesha Shastri
Rakesha Shastri

Reputation: 11243

You have to call the present inside the completion handler of the dismiss.

func readerDidCancel(_ reader: QRCodeReaderViewController) {

    weak var presentingViewController = self.presentingViewController

    self.dismiss(animated: true, completion: {
        presentingViewController?.present(ClockInOrOutViewController(), animated: true, completion: nil)
    })
}

If this does not work, it means your presenting view controller has also been removed somehow. (dismissed/popped?)

Upvotes: 2

Konrad Piękoś
Konrad Piękoś

Reputation: 194

You can't present view controller while other view controller is dismissing and also present on dismissing view controller. You can do something like this:

func readerDidCancel(_ reader: QRCodeReaderViewController) {
   let presenting = self.presentingViewController
   dismiss(animated: true, completion: {
      presenting?.present(ClockInOrOutViewController(), animated: true, completion: nil)
   }) 
}

Upvotes: 0

Related Questions