Zebraプリンタに奇妙な問題があります。大きな画像では、キューから1つずつ印刷する項目を取得するコードがあります。したがって、印刷が開始され、キューに3つのアイテムがある場合、コードはキュー内の最初のデータをループして取り出し、プリンタに送信し、キュー内の最初のデータを削除します。並べ替えのような並べ替え。Zebraプリンタは、ユーザがタップした場合、連続した複数のラベルのみを印刷します。
問題は、ループしてデータをプリンタに直接送信するコードの場合、プリンタは最初の項目のみを印刷することです。 NSLogは、1つのアイテムごとに、プリンタ接続のオープン、データの送信、印刷の成功、プリンタの接続が閉じられたことを示していますが、次の項目はなくなりました。
しかし、コードが1つのラベルを印刷するたびに「次のラベルを印刷するためにOKを押してください」というメッセージボックスが表示されたら、ユーザーは[OK]ボタンをタップして2番目と残りのボタンをタップするたびにラベルを表示します。
これをエミュレートしようとしました。私は "ボタンをプログラムでプッシュ"するタイマを使用しようとしました[btnPrint sendActionsForControlEvents:UIControlEventTouchUpInside]
、私はまた、関数を直接呼び出して、またはスレッドに遅延を与えるためにタイマを使用しますが、何も動作しません。それは、人間のタッチからタップしたボタンから開始されなければならない。どうしてか分かりません。ここで
はコードです:
// main function to print
-(void) printLabel {
if ([dataToPrint count] > 0) {
[self printWithData:[dataToPrint objectAtIndex:0]];
}
}
-(void)printWithData:(NSString*) data;
{
NSString *zplString = data;
// do something with zplString
NSLog(@"Sending data to printer");
printHandler = [[PrintingHandler alloc] init];
[printHandler setDelegate:self];
[printHandler initialize];
[printHandler printToSerial:bluetoothSerialNumber withData:zplString];
}
// delegate to call if the print is success
-(void) printIsSuccess
{
[dataToPrint removeObjectAtIndex:0];
// in here, I just use sleep code instead of button tap emulation to avoid unnecessarily too long code
[NSThread sleepForTimeInterval:2.0f];
[self printLabel];
}
// this is method inside PrintingHandler class that get called by PrintingHandler (printToSerial:)
-(void) printLabelWithData:(NSString*) zplData toPrinter:(NSString*) serial withSender:(id) sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
// Instantiate connection to Zebra Bluetooth accessory
id<ZebraPrinterConnection, NSObject> thePrinterConn = [[MfiBtPrinterConnection alloc] initWithSerialNumber:serial];
// Open the connection - physical connection is established here.
BOOL success = [thePrinterConn open];
NSError *error = nil;
// Send the data to printer as a byte array.
success = success && [thePrinterConn write:[zplData dataUsingEncoding:NSUTF8StringEncoding] error:&error];
[NSThread sleepForTimeInterval:1.0f];
//Dispath GUI work back on to the main queue!
dispatch_async(dispatch_get_main_queue(), ^{
if (success != YES || error != nil) {
[delegate printFailed];
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
else if (success != YES) {
NSLog(@"Print is not success, but no error raised");
[delegate printSuccess];
}
else
{
NSLog(@"Print is success");
[delegate printSuccess];
}
});
// Close the connection to release resources.
NSLog(@"printer connection closed");
[thePrinterConn close];
[thePrinterConn release];
});
}