DZNWebViewController icon indicating copy to clipboard operation
DZNWebViewController copied to clipboard

Crashes when closing it as a modal

Open jaybuangan opened this issue 8 years ago • 13 comments

I have a tableview with 3 rows each with a button. When the user clicks on the button it opens up your view controller in a modal. What's weird is that it'll work fine if I open the modal when clicking on the button from the second or third row but when I try to click on the button on the first row it crashes with this error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x126162bf0 of class UINavigationBar was deallocated while key value observers were still registered with it.

<NSKeyValueObservationInfo 0x12607c780> ( <NSKeyValueObservance 0x126016bf0: Observer: 0x126166db0, Key path: hidden, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10023fb18, Property: 0x1261319f0> <NSKeyValueObservance 0x12606c9d0: Observer: 0x126166db0, Key path: center, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10023fb18, Property: 0x1261325a0> <NSKeyValueObservance 0x12606cae0: Observer: 0x126166db0, Key path: alpha, Options: <New: YES, Old: NO, Prior: NO> Context: 0x10023fb18, Property: 0x126132ad0> )'

this is the code i'm using to open the URL:

var webModalNC: UINavigationController!
func showSource(url: String) {

        let vc = DZNWebViewController(URL: NSURL(string: url)!)
        self.webModalNC = UINavigationController(rootViewController: vc)
        vc.supportedWebNavigationTools = DZNWebNavigationTools.All
        vc.supportedWebActions = DZNsupportedWebActions.DZNWebActionAll
        vc.showLoadingProgress = true
        vc.hideBarsWithGestures = true
        vc.showPageTitleAndURL = true

        let closeButton = UIBarButtonItem(title: "Close", style: .Plain, target: self, action: #selector(HomeViewController.closeWebModal))
        vc.navigationItem.rightBarButtonItem = closeButton

        self.presentViewController(webModalNC, animated: true, completion: nil)

    }

    func closeWebModal(){

        self.webModalNC.dismissViewControllerAnimated(true, completion: nil)

    }

Do i need to remove observers manually before dismissing the modal?

jaybuangan avatar Mar 28 '16 22:03 jaybuangan

Are these lines called when you dismiss the modal? https://github.com/dzenbot/DZNWebViewController/blob/e1731d96ffaa5cbdce289b5e2948b9f805d92a5c/Source/Classes/DZNWebViewController.m#L858-L862

PS: You shouldn't have to remove observers yourself. It should do it internally.

dzenbot avatar Apr 11 '16 17:04 dzenbot

It crashes when I show the modal. And it happens at random times. Sometimes I can show the modal for a particular link 2 or 3 times before eventually crashing.

Unfortunately Xcode only says it's crashes at the appdelegate class, so I can't give you anymore information:

class AppDelegate: UIResponder, UIApplicationDelegate {

jaybuangan avatar Apr 11 '16 21:04 jaybuangan

Try adding an exception breakpoint. https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html

Unless you're using a dynamic framework, it should still point out where the exception is being triggered.

dzenbot avatar Apr 11 '16 21:04 dzenbot

It still showing that same line. I tried to debug step by step and it crashes when i present the viewcontroller.

self.presentViewController(webModalNC, animated: true, completion: nil)

jaybuangan avatar Apr 11 '16 21:04 jaybuangan

You should try weak referencing webModalNC..

xradeon avatar Apr 11 '16 22:04 xradeon

Doesn't look like webModalNC is a property for him. Is it?

dzenbot avatar Apr 11 '16 23:04 dzenbot

I declared webModalNC as weak at the top

'weak var webModalNC: UINavigationController?'

and now it's always nil even after initializing the UINavigationController in showSource().

jaybuangan avatar Apr 11 '16 23:04 jaybuangan

Does it need to be a var? Can't you just make it a let in your implementation scope? Try sharing a gist of your implementation as it is hard to help by guessing..

dzenbot avatar Apr 11 '16 23:04 dzenbot

var webModalNC: UINavigationController?

func showSource(url: String) {

    let vc = DZNWebViewController(URL: NSURL(string: url)!)
    self.webModalNC = UINavigationController(rootViewController: vc)
    vc.supportedWebNavigationTools = DZNWebNavigationTools.All
    vc.supportedWebActions = DZNsupportedWebActions.DZNWebActionAll
    vc.showLoadingProgress = true
    vc.hideBarsWithGestures = true
    vc.showPageTitleAndURL = true

    let closeButton = UIBarButtonItem(title: "Close", style: .Plain, target: self, action: #selector(HomeViewController.closeWebModal))
    vc.navigationItem.rightBarButtonItem = closeButton

    self.presentViewController(webModalNC!, animated: true, completion: nil)
}

func closeWebModal() {

    self.webModalNC!.dismissViewControllerAnimated(true, {
        self.webModalNC = nil
    })
}

xradeon avatar Apr 11 '16 23:04 xradeon

not sure if i'm doing this right..

https://gist.github.com/jaybuangan/1a8c020ac2b2b213928bc2afa865be22

i took out all the tableview code.

basically there's a view in a tableviewcell that when clicked, calls a delegate function "showSource" which is defined in this view controller. It's just passing a URL string.

jaybuangan avatar Apr 11 '16 23:04 jaybuangan

Also crashes for me, same scenario as above. I am showng it as a modal, crashes on exit with debugger pointing to AppDelegate.

nmvictor avatar Apr 14 '16 13:04 nmvictor

i have the same issue while dismiss view controller

O-mkar avatar Nov 01 '16 04:11 O-mkar

Same issue here, crashing on clients but i cant debug because crashlytics dont give enough info for this crash.

Fatal Exception: NSInternalInconsistencyException An instance 0x14dd45670 of class UINavigationBar was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x170229100> ( <NSKeyValueObservance 0x17005fd70: Observer: 0x14dd24c00, Key path: hidden, Options: <New: YES, Old: NO, Prior: NO> Context: 0x1002c76a8, Property: 0x170251ca0> <NSKeyValueObservance 0x1702585d0: Observer: 0x14dd24c00, Key path: center, Options: <New: YES, Old: NO, Prior: NO> Context: 0x1002c76a8, Property: 0x170251f40> <NSKeyValueObservance 0x170252660: Observer: 0x14dd24c00, Key path: alpha, Options: <New: YES, Old: NO, Prior: NO> Context: 0x1002c76a8, Property: 0x170251d00> )

Cayke avatar Sep 09 '17 16:09 Cayke