2013-04-09 60 views
-3

進出口使用的zbar和閱讀器SDK簡單,如果在Xcode中陳述

.H

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
// ADD: delegate protocol 
<ZBarReaderDelegate> 
{ 
UIImageView *resultImage; 
UITextView *resultText; 
UILabel *text; 
} 
@property (nonatomic, retain) IBOutlet UIImageView *resultImage; 
@property (nonatomic, retain) IBOutlet UITextView *resultText; 
@property (nonatomic, retain) IBOutlet UILabel *text; 
- (IBAction) scanButtonTapped; 

@end 

UITextView *resultText; 
IBOutlet UILabel *text; 

.M

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize resultImage, resultText, text; 

- (IBAction) scanButtonTapped 
{ 
// 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]; 

} 





- (void) imagePickerController: (UIImagePickerController*) reader 
didFinishPickingMediaWithInfo: (NSDictionary*) info 
{ 
// ADD: get the decode results 
id<NSFastEnumeration> results = 
[info objectForKey: ZBarReaderControllerResults]; 
ZBarSymbol *symbol = nil; 
for(symbol in results) 
    // EXAMPLE: just grab the first barcode 
    break; 

// EXAMPLE: do something useful with the resulting data 
resultText.text = symbol.data; 


//Below are the IF Statements... 
if ((symbol = @"3307210410801")) { 
    text.text = @"FarCry 2"; 
} 
else if ((symbol = @"530917119347")) { 
    text.text = @"Call of Duty: Black Ops 2"; 
} 
else if ((symbol = @"5021290053694")) { 
    text.text = @"Hitman Absolution"; 
} 
else if ((symbol = @"5026555401739")) { 
    text.text = @"Red Dead Redemption"; 
} 




// EXAMPLE: do something useful with the barcode image 
resultImage.image = 
[info objectForKey: UIImagePickerControllerOriginalImage]; 

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




- (void) dealloc 
{ 
self.resultImage = nil; 
self.resultText = nil; 
self.text = nil; 


} 


- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation 
{ 
return(YES); 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

它的工作原理,這樣當resultText顯示在我的模擬器爲5021290053694,然後'文字'顯示「Pingu」

我的問題是Pingu在下一個數字出現時不會消失,所以當50 26555401739顯示「文字」應顯示「死亡肖恩」。相反,它仍然是Pingu。

換句話說,它不會取消分配首先顯示的任何「文本」。第一個將留在那裏,直到我關閉應用程序並重新打開。

希望這很容易理解。 :)

預先感謝您。

  • 尼克

編輯

回答

3

你不應該比較字符串直接這樣。它將比較引用而不是值。使用[symbol isEqualToString:@"3307210410801"]代替

+0

感謝您的回覆迅速即時得到「的ZBarSymbol沒有明顯@interface聲明選擇器「isEqualToString。」 我編輯了我的問題,以顯示更多的即時通訊工作。 乾杯 – 2013-04-09 16:33:47

2

您正在使用=這是一個賦值運算符。

==是一個比較運算符。

但是比較需要isEqualTo:兩個對象,它如果是字符串,你應該做的isEqualToString:

你的代碼應該是:

if ([symbol isEqualToString:@"3307210410801"]) { 
    text.text = @"LOTR"; 
} 
else if ([symbol isEqualToString: @"530917119347"]) { 
    text.text = @"James Bond"; 
} 
else if ([symbol isEqualToString: @"5021290053694"]) { 
    text.text = @"Pingu"; 
} 
else if ([symbol isEqualToString: @"5026555401739"]) { 
    text.text = @"Shaun of the Dead"; 
} 
+0

我得到了'No Visible @interface for ZBarSymbol聲明瞭選擇器「isEqualToString。」 我在編輯問題以更好地理解我的實現文件的外觀。 感謝您的迅速回復 – 2013-04-09 16:26:19

+1

獲取字符串中的文本,然後使用isequaltostring: – 2013-04-09 16:32:14

+0

ur zbarsymbol是某種自定義對象....將其字符串值取出。 – 2013-04-09 16:39:36