1
votes

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)
}
2
You can't do this.You should present new controller only when previous one is fully dismissed.Vikky
@Vikky How do you propose I do this?Minimoore26
I thought Konrad Piękoś's answer would be working.But it didn't as you comment suggests.So now you have to use protocols and delegates to solve your problem.Take a look at medium.com/swift2go/…Vikky

2 Answers

1
votes

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?)

0
votes

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)
   }) 
}