0

UIImageViews2と 2で構成される単純なアプリケーションを作成していますUIButtons。はのUIImageViews上に配置されUIButtonsます。

UIImageランダムに表示され、UIImageView2 秒後に消える1 つがあります。次に、ユーザーは、画像が表示されたと思われるボタンをタップする必要があります。

配列をシャッフルしUIImageView、最初の要素 (インデックス 0) を使用して画像を表示します。シャッフルするとき、配列のインデックス 0 に配置された要素 (つまり、どのインデックスから、つまりインデックス "n" から) を追跡します。

ただし、ボタンが押されると、押されたボタンの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 に答える