【问题标题】:Simple IF Statements in XcodeXcode 中的简单 IF 语句
【发布时间】:2013-04-09 16:04:03
【问题描述】:

我正在使用 ZBar 阅读器 SDK

.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 时,'text' 显示“Pingu”

我的问题是 Pingu 不会在下一个数字出现时消失,所以当 5026555401739 显示时,“文本”应该显示“死者肖恩”。相反,它仍然是 Pingu。

换句话说,它不会释放首先显示的“文本”。第一个将留在那里,直到我关闭应用程序并重新打开。

希望这很容易理解。 :)

提前谢谢你。

  • 尼克

编辑

【问题讨论】:

    标签: objective-c if-statement uilabel uitextview


    【解决方案1】:

    你不应该像那样直接比较字符串。它将比较引用而不是值。请改用[symbol isEqualToString:@"3307210410801"]

    【讨论】:

    • 感谢您的快速回复,我收到“ZBarSymbol 没有可见的@interface 声明选择器“isEqualToString”。我已经编辑了我的问题以显示更多我正在使用的内容。干杯
    【解决方案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";
    }
    

    【讨论】:

    • 我得到 'No Visible @interface for ZBarSymbol 声明选择器“isEqualToString”。我正在编辑问题以更好地了解我的实现文件的外观。感谢您的快速回复
    • 获取字符串中的文本,然后使用 isequaltostring:
    猜你喜欢
    • 2020-02-22
    • 2019-03-17
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多