0

我正在编写一个由 2UIImageViews和 2组成的简单应用程序UIButtons。被UIImageViews放置在UIButtons.

我有 1UIImage随机出现UIImageView并在 2 秒后消失。然后用户必须点击他们认为出现图像的按钮。

我打乱UIImageView数组并使用第一个元素(索引 0)作为要显示的图像。改组时,我会跟踪哪个元素(即来自哪个索引。假设来自索引“n”)被放置在数组的索引 0 上。

但是,当按下按钮时,我将按下按钮的 id 与索引为 n 的按钮的 id 进行比较。(因为那是 random 的索引UIImageView)。但由于某种原因,它不起作用。:(

这是我的代码:(我没有上传 .h 文件。它只包含声明。)下面的代码不会产生任何错误/警告消息。它只是没有输出我想要的结果。

@interface ViewController ()
@property (strong, nonatomic) NSMutableArray* tempButton;
@property (strong, nonatomic) NSMutableArray *tempViews;
@property (strong, nonatomic) UIImage *myImage;
@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

     _myImage = [UIImage imageNamed:@"hello"];
    _tempButton = [[NSMutableArray alloc] initWithObjects:_button1, _button2, nil];

    _tempViews = [[NSMutableArray alloc]initWithObjects:_image1, _image2, nil];


     [self displayonView];

}



-(void)displayToFindSymbol{
    [_toFindImage setImage:_myImage];
    _toFindImage.contentMode = UIViewContentModeScaleAspectFit;
}

- (IBAction)pressed:(id)sender {

    UIButton *result = (UIButton *)sender;
    if (result == _tempButton[_correctButton]) {
       NSLog (@"Correct");
    }
    else if (result != _tempButton[_correctButton]){
       NSLog (@"Incorrect");            

    }

}



-(NSMutableArray *)shuffleViews{
    NSUInteger count = _tempViews.count;
    int n;
    for (int i=count-1; i>=0; --i) {

        n = arc4random()  % (i + 1);


        [_tempViews exchangeObjectAtIndex:n withObjectAtIndex:i];
    }
    _correctButton = n;
    return _tempViews;

}

-(void)displayonView{

    NSMutableArray *tempArray;
    tempArray = [self shuffleViews]; // shuffle views

     _correctImage = [tempArray objectAtIndex:0];

    _correctImage.contentMode = UIViewContentModeScaleAspectFit;

    [_correctImage setImage:_myImage];

    NSTimer* myTImer;
    myTImer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hideLabel) userInfo:nil repeats:NO];

}

-(void) hideLabel{
    _correctImage.hidden = YES;
    for (UIButton *each in self.tempButton) {
        each.enabled = YES;
    }
    [self displayToFindSymbol];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
4

2 回答 2

0

我对objective-c和iOS也有点陌生,但我认为你不应该这样比较:

result == _tempButton[_correctButton]

试试这个比较:

[result isEqual:_tempButton[_correctButton]]
于 2013-09-17T13:55:00.157 回答
0

改为使用标签,为两个按钮设置一个唯一的标签,并将图像的标签设置为图像所属的按钮,稍后比较图像和按钮的标签以检查它们是否相同.

https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instp/UIView/tag

于 2013-09-17T12:50:37.157 回答