我试图保存这样的UIPrinter对象。

-(IBAction)setPrinterInSettings:(id)sender{

  UIPrinterPickerController *picker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:self.SavedPrinter];
  [picker presentFromRect:self.settingsButton.frame inView:self.view animated:YES completionHandler:^(UIPrinterPickerController *controller, BOOL userDidSelect, NSError *err) {
    if (userDidSelect) {
      self.SavedPrinter = controller.selectedPrinter;
      NSLog(@"self.SavedPrinter == %@",self.SavedPrinter);

      [self saveCustomObject:self.SavedPrinter key:@"SavedPrinter"];

          [[NSUserDefaults standardUserDefaults] setObject:self.SavedPrinter forKey:@"SavedPrinter"];
          [[NSUserDefaults standardUserDefaults] synchronize];
    }

  }];

}


当我尝试保存在[[NSUserDefaults standardUserDefaults] setObject:self.SavedPrinter forKey:@"SavedPrinter"];时,它崩溃了,并且显示此消息为错误-[UIPrinter encodeWithCoder:]: unrecognized selector sent to instance 0x7fb204062760。我需要它来记住用户选择的打印机,并且即使应用程序退出后台也要记住它。如果有人可以帮助,那就太好了!

最佳答案

您不能在UIPrinter中存储NSUserDefaults实例。您可以做的就是保存打印机的URL,然后在您的应用再次启动且需要UIPrinter时,从NSUserDefaults读取URL并使用UIPrinter printerWithURL:

关于ios - 将UIPrinter保存到NSUserDefaults,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37174540/

10-09 09:37