0

I have a following helper class

class PDFPreviewHelper {
    var pdfNavigationController: UINavigationController!
    func previewButtonPressed(rootViewController: UIViewController) {
        let pdfViewController = PDFViewController(resource: "final.pdf")
        pdfNavigationController = UINavigationController(rootViewController: pdfViewController)

        let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backButtonPressedInPDF")
        pdfViewController.navigationItem.setLeftBarButtonItem(backButton, animated: false)

        rootViewController.presentViewController(pdfNavigationController, animated: true, completion: nil)
    }



    func backButtonPressedInPDF() {
        pdfNavigationController.dismissViewControllerAnimated(true, completion: nil)
    }
}

I call a function in above helper class in my rootviewcontroller like following:

func previewInPdfButtonPressed() {
    let a = PDFPreviewHelper()
    a.viewI129InPDF(self)
}

I successfully modally present pdfNavigationController on top of my rootViewController, but whenever i press back button, nothing gets called. Why is this so? I set a break point in backButtonPressedInPDF function and it doesn't even hit the break point.

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
Kahsn
  • 1,045
  • 3
  • 15
  • 25

4 Answers4

0

I think the problem lies here.

let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backButtonPressedInPDF")

To:

let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backButtonPressedInPDF:")

I believe the problem as at action: "backButtonPressedInPDF" where there is a missing :

In addition, change your method by adding (sender:AnyObject) also?

func backButtonPressedInPDF(sender:AnyObject) {
        pdfNavigationController.dismissViewControllerAnimated(true, completion: nil)
    }
jo3birdtalk
  • 616
  • 5
  • 20
0

You need to assign leftBarButtonItem to UINavigationController via below way.

pdfViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backButtonPressedInPDF")
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
0

The issue is you are creating a temporary instance of PDFPreviewHelper class and using it to present the view controller. Not keeping the reference. This can be fixed in two ways:

Method 1: Add previewInPdfButtonPressed method to your PDFViewController class. Then change implementation like following.

let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: pdfViewController, action: "backButtonPressedInPDF")

Method 2: You should keep the object a until you dismiss your PDFViewController-navigationcontroller. Even if you do like this you will get an exception like

2016-03-16 15:43:03.140 XXXX[3757:585277] *** NSForwarding: warning: object 0x7ff5eb72be70 of class 'XXXX.PDFPreviewHelper' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[XXXX.PDFPreviewHelper backButtonPressedInPDF]

This is because you are not subclassing NSObject for your PDFPreviewHelper. You can fix it in two ways,

  1. Make PDFPreviewHelper as NSObject subclass.
  2. Add dynamic keyword to the function like dynamic func previewInPdfButtonPressed() {....}.

Resolution to this issue is well described here

Community
  • 1
  • 1
Johnykutty
  • 12,091
  • 13
  • 59
  • 100
0
func previewInPdfButtonPressed() 
{
    let a = PDFPreviewHelper()
    a.viewI129InPDF(self)
}

The problem is a is released when the program goes out of the scope of this function. The action you target on a of course can't be executed anymore

solution add a as a property of the viewController

bunnyshell
  • 253
  • 4
  • 12