如何延迟调用方法,直到其他代码先完成

时间:2013-09-18 10:11:58

标签: iphone ios objective-c

我想延迟调用方法,直到其他代码先完成。

这是按钮触发的动作。用户扫描条形码。在我打电话给另一种方法之前,我希望这已经完成:[self performSelector:@selector(otherMethod:) withObject:nil afterDelay:0.5]; 我尝试使用延迟,但这并不好,不可预测。

    - (IBAction) scanButtonTapped
{
    NSLog(@"TBD: scan barcode here...");

    // ADD: present a barcode reader that scans from the camera feed
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;
    reader.supportedOrientationsMask = ZBarOrientationMaskAll;

    ZBarImageScanner *scanner = reader.scanner;
    // TODO: (optional) additional reader configuration here

    // EXAMPLE: disable rarely used I2/5 to improve performance
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];

    // present and release the controller
    [self presentModalViewController: reader
                            animated: YES];

    [self performSelector:@selector(otherMethod:) withObject:nil afterDelay:0.5];


}

2 个答案:

答案 0 :(得分:3)

也许您需要使用委托方法

- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
   //call other method here
}

我假设你正在使用ZBar库

答案 1 :(得分:1)

您不会显示您的扫描代码,但我认为您需要在扫描完成时(成功或其他方式)将其传递给阻止

想象一下扫描方法如下所示:

- (void)scanBarCodeWithCompletionBlock:^(BOOL successfulScan)completionBlock {

    if (do the scan) {
        completionBlock(YES);
    } else {
        completionBlock(NO);
    }
}

您可能希望通过属性将完成块传递给扫描程序代码,并根据需要传递并调用它:

ZBarImageScanner *scanner = reader.scanner;
scanner.completionBlock = ^(BOOL successfulScan) {
    if (!successfulScan) {
         initiateMoaningSequence();
    }
};