0

Thanks to a kind stackoverflow member I now have AirPrint(ing) working perfectly from the iPhone, but the function call for the iPad is giving me fits. Here is what I have:

    if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        pic.presentFromRect(self.view.frame, inView:self.view, animated:true, completionHandler: nil)
    } else {
        pic.presentAnimated(true, completionHandler: nil)
    }

The line that calls the print dialog for the iPad is my Swift conversion of Objective-C code that I found numerous times through my Google searching on the topic. It runs and gets called correctly, but all it does is appear to "dim" the iPad screen on the simulator (like something is opening, but it's just a huge grey transparent rectangle that covers the entire screen). If I click anywhere on the dimmed screen it returns to normal and the program continues as if I never pressed my "Print" button (I am using my own "Print" button I created in the Storyboard). I can only test this on the simulator currently, so it could even be a beta bug for all I know... but I'm guessing I'm doing something wrong.

Any suggestions are appreciated!

Thanks.


Thanks to Aaron, my printer select pop-up for the iPad now works on every button in my program except the print button! :) Here is the relevant code:

@IBOutlet var myButton1: BorderedButton!
@IBOutlet var myButton2: BorderedButton!
@IBOutlet var myButton3: BorderedButton!

Works perfectly on all but button 3, which of course is my "Print" button. Here is the function for button 3:

@IBAction func button3Tapped() {
    var pic:UIPrintInteractionController = .sharedPrintController()
    var viewpf:UIViewPrintFormatter = myTextView.viewPrintFormatter()
    pic.delegate = self
    pic.showsPageRange = true
    pic.printFormatter = viewpf
    if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        pic.presentFromRect(self.myButton3.frame, inView:self.view, animated:true, completionHandler: nil)
    } else {
        pic.presentAnimated(true, completionHandler: nil)
    }
}

Change that "self.myButton3.frame" to myButton1 or myButton2 and I get the printer select pop-up in the iPad simulator with no problems. But making it myButton3 just gives me the error "fatal error: unexpectedly found nil while unwrapping an Optional value". I'm willing to let it appear on one of the other buttons if I have no other choice, but it would probably be good for me to understand what in the heck is happening!

Thanks again!

Jim Barber
  • 2,139
  • 2
  • 13
  • 8
  • Change `self.view.frame` to `self.view.bounds` – Aaron Brager Jul 27 '14 at 00:23
  • See http://stackoverflow.com/questions/1210047/cocoa-whats-the-difference-between-the-frame-and-the-bounds – Aaron Brager Jul 27 '14 at 00:24
  • Unfortunately that change just gave me the exact same results, but you have given me something more to research (starting with that other post you reference) and I really appreciate it! Thanks! – Jim Barber Jul 27 '14 at 02:00
  • Does `self.view` take up the whole screen? Is there a button or UI element it makes sense to present from? (like `presentFromRect(self.printButton.frame, inView:self.view`)? – Aaron Brager Jul 27 '14 at 15:03
  • Well that is interesting! I have three buttons in a row together. If I specify either of the other two buttons as you mention above the printer select pops up just fine! But when I use the actual "Print" button I created, I get the error "fatal error: unexpectedly found nil while unwrapping an Optional value". I'm going to post an addition to my original post with the relevant code. Thanks again for your help with this! – Jim Barber Jul 27 '14 at 15:40
  • Either your outlet for `myButton3` isn't connected to your button (try reconnecting it) or it's getting set to `nil` later. (If you *expect* that it might be `nil` for some reason, it should be declared as optional like `@IBOutlet var myButton3: BorderedButton?`, but this would be abnormal). – Aaron Brager Jul 27 '14 at 16:25
  • You are correct... it was missing the "Referencing Outlets" connection! Works just fine now. Thanks again! – Jim Barber Jul 27 '14 at 18:34

1 Answers1

0

Either your outlet for myButton3 isn't connected to your button (try reconnecting it) or it's getting set to nil later, in which case it should be declared as optional: @IBOutlet var myButton3: BorderedButton?.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • My other reply probably blended into the comments... you were correct, it was missing the "Referencing Outlets" connection! Works fine now. Thanks again! – Jim Barber Jul 29 '14 at 02:59