Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Crashes when closing it as a modal #45

Open
jaybuangan opened this issue Mar 28, 2016 · 13 comments
Open

Crashes when closing it as a modal #45

jaybuangan opened this issue Mar 28, 2016 · 13 comments

Comments

@jaybuangan
Copy link

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?

@dzenbot
Copy link
Owner

dzenbot commented Apr 11, 2016

Are these lines called when you dismiss the modal?

if (self.hideBarsWithGestures) {
[self.navigationBar removeObserver:self forKeyPath:@"hidden" context:&DZNWebViewControllerKVOContext];
[self.navigationBar removeObserver:self forKeyPath:@"center" context:&DZNWebViewControllerKVOContext];
[self.navigationBar removeObserver:self forKeyPath:@"alpha" context:&DZNWebViewControllerKVOContext];
}

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

@jaybuangan
Copy link
Author

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 {

@dzenbot
Copy link
Owner

dzenbot commented Apr 11, 2016

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.

@jaybuangan
Copy link
Author

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)

@xradeon
Copy link

xradeon commented Apr 11, 2016

You should try weak referencing webModalNC..

@dzenbot
Copy link
Owner

dzenbot commented Apr 11, 2016

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

@jaybuangan
Copy link
Author

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().

@dzenbot
Copy link
Owner

dzenbot commented Apr 11, 2016

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..

@xradeon
Copy link

xradeon commented Apr 11, 2016

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

@jaybuangan
Copy link
Author

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.

@nmvictor
Copy link

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

@O-mkar
Copy link

O-mkar commented Nov 1, 2016

i have the same issue while dismiss view controller

@Cayke
Copy link

Cayke commented Sep 9, 2017

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants