Objective C 使用 ZBar 扫描二维码后无法切换视图

发布于 2024-12-19 18:37:38 字数 1409 浏览 1 评论 0原文

我正在使用 XCode 4.2 开发一个检测 QR 码的应用程序。

我试图在二维码检测后切换视图,但它根本不起作用,

这里是正在使用的代码:

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{

     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);


    // ADD: get the decode results
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;   
    for(symbol in results)
        break;


    NSString *string=symbol.data;
    NSString *string2=@"1234";

    if ([string isEqualToString:string2]) {

//this is the part that is not working : it doesn t load the AboutView at all

        AboutView *about = [[AboutView alloc] initWithNibName:nil bundle:nil];
        [self presentModalViewController:about animated:YES];
    }

    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                        message:@"This is not a recognized QR code!" 
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];

    }

    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];
}

谢谢

I am developping an App using XCode 4.2 that detects a QR code.

I am trying to make a switch view after QR code detection but it is not working at all

here is the code am using :

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{

     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);


    // ADD: get the decode results
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;   
    for(symbol in results)
        break;


    NSString *string=symbol.data;
    NSString *string2=@"1234";

    if ([string isEqualToString:string2]) {

//this is the part that is not working : it doesn t load the AboutView at all

        AboutView *about = [[AboutView alloc] initWithNibName:nil bundle:nil];
        [self presentModalViewController:about animated:YES];
    }

    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                        message:@"This is not a recognized QR code!" 
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];

    }

    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];
}

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2024-12-26 18:37:38

问题是 reader 是 zbar 示例代码中呈现的视图控制器

-(void)presentReaderInViewController:(UIViewController*)vc

,并且您将 self 视为呈现的

您应该使用 reader 来呈现您的 AboutView 并仅关闭else 块中的 reader

if ([string isEqualToString:string2]) {

//this is the part that is not working : it doesn t load the AboutView at all

        AboutView *about = [[AboutView alloc] initWithNibName:nil bundle:nil];
        [reader presentModalViewController:about animated:YES];
    }

    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                        message:@"This is not a recognized QR code!" 
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];
        // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];
    }

您可能还想等待在警报视图的委托方法中关闭 reader (创建一个软引用并关闭该... myReader = reader ; 当您设置警报视图时)

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    [myReader dismissModalViewControllerAnimated: YES];

}

The issue is that the reader is the presented view controller in zbar's example code

-(void)presentReaderInViewController:(UIViewController*)vc

and you are treating self as if it is presented

You should use reader to present your AboutView and only dismiss reader in the else block

if ([string isEqualToString:string2]) {

//this is the part that is not working : it doesn t load the AboutView at all

        AboutView *about = [[AboutView alloc] initWithNibName:nil bundle:nil];
        [reader presentModalViewController:about animated:YES];
    }

    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                        message:@"This is not a recognized QR code!" 
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];
        // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];
    }

You might also want to wait to dismiss reader in the delegate method of your alert view (create a soft reference and dismiss that... myReader = reader; when you set up the alertview)

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    [myReader dismissModalViewControllerAnimated: YES];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文