Nurdin
Nurdin

Reputation: 23883

Swift - Warning: Attempt to present * on * while a presentation is in progress

I can't switch to another view controller because of this error. I want to switch to another view controller after it successfully scan the QR code.

2015-01-27 17:59:16.093 *[5416:1860144] Warning: Attempt to present <*.SuccessViewController: 0x144e38a20> on <*.MethodViewController: 0x144e2b960> whose view is not in the window hierarchy!

My code

@IBAction func btnQrCode(sender: AnyObject) { 
    reader.modalPresentationStyle = .FormSheet
    reader.delegate               = self

    reader.completionBlock = { (result: String?) in
    if result != nil {
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let successViewController = storyBoard.instantiateViewControllerWithIdentifier("successView") as SuccessViewController
        self.presentViewController(successViewController, animated:true, completion:nil)

    }

    presentViewController(reader, animated: true, completion: nil)
    }
}

P.S: I'm using QRCodeReader plugin, https://github.com/yannickl/QRCodeReader.swift.

Upvotes: 2

Views: 5509

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

Can you try showing the modal view controller after the reader(QRCodeReader) has been dismissed. There is a callback for QRCodeReader called didScanResult in the documentation.

https://github.com/yannickl/QRCodeReader.swift

func reader(reader: QRCodeReader, didScanResult result: String) {
   self.dismissViewControllerAnimated(true, completion: { () -> Void in

       // use the result variable here.

        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let successViewController = storyBoard.instantiateViewControllerWithIdentifier("successView") as SuccessViewController
        self.presentViewController(successViewController, animated:true, completion:nil)

   })
}

Remove the code from reader.completionBlock.

Upvotes: 2

Related Questions