Мне нужно получить доступ к NSArray и получить из него значение с помощью:
-(IBAction) randClicked:(id)sender
{
int randNum;
//when user clicks the button
//create random number, 1 - 32
randNum = arc4random() %32;
Как видите, я могу отобразить мое случайное число здесь:
///using this code for testing random number when button is clicked
NSString *randValText = [[NSString alloc]initWithFormat:@"%d", randNum];
//Output random number to label
labelRandText.text = randValText;
//Call method seeImage with SEL methodSelector
SEL methodSelector = @selector(seeImage);
///set image opacity to 0
wisdomView.alpha = 0.0;
//Use NSTimer to call method
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:methodSelector userInfo:nil repeats:NO];
[randValText release];
}
Но теперь мне нужно сравнить значение randNum и получить доступ к NSArray изображений для его положения в массиве, чтобы я мог отобразить изображение в UIImageView.
Трудно понять это. Вся помощь очень ценится. Спасибо за ваше время.
Вот текущий код, который я реализовал с помощью Джоша по состоянию на 11:13 вечера 6 июня 2001 года. Я думаю, что почти у меня это есть ... но то, что я делаю, не работает. Я продолжаю получать предупреждение "Method" -objectAtIndex не найден (тип возвращаемого значения по умолчанию равен 'id')
//Call method seeImage with SEL methodSelector
SEL methodSelector = @selector(seeImage);
//Use NSTimer to call method
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:methodSelector userInfo:[NSNumber numberWithInt:randNum] repeats:NO];
[randValText release];
}
////This is the method to make the graphic choosen by randNum to display at the top of the screen
-(IBAction) seeImage: (NSTimer *)tim
{
CGContextRef imageContext = UIGraphicsGetCurrentContext();
int randNum = [(NSNumber *)[tim userInfo] intValue];
UIImage *imgToDisplay = [wisdomView objectAtindex:randNum];
wisdomView.image = imgToDisplay;
wisdomView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"0.png"],
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"],
[UIImage imageNamed:@"10.png"],
[UIImage imageNamed:@"11.png"],
[UIImage imageNamed:@"12.png"],
[UIImage imageNamed:@"13.png"],
[UIImage imageNamed:@"14.png"],
[UIImage imageNamed:@"15.png"],
[UIImage imageNamed:@"16.png"],
[UIImage imageNamed:@"17.png"],
[UIImage imageNamed:@"18.png"],
[UIImage imageNamed:@"19.png"],
[UIImage imageNamed:@"20.png"],
[UIImage imageNamed:@"21.png"],
[UIImage imageNamed:@"22.png"],
[UIImage imageNamed:@"23.png"],
[UIImage imageNamed:@"24.png"],
[UIImage imageNamed:@"25.png"],
[UIImage imageNamed:@"26.png"],
[UIImage imageNamed:@"27.png"],
[UIImage imageNamed:@"28.png"],
[UIImage imageNamed:@"29.png"],
[UIImage imageNamed:@"30.png"],
[UIImage imageNamed:@"31.png"],nil];
wisdomView.alpha = 0;
[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 1;
[UIView commitAnimations];
SEL methodSelector = @selector(hideImage);
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:methodSelector userInfo:nil repeats:NO];
}
Я ценю помощь. Спасибо за ваше время.
Вот измененный код, который все еще не работает .... Я чего-то не понимаю. 13:19 7/7/2011
-(IBAction) seeImage:(NSTimer *)tim
{
NSArray *wisdom;
wisdom = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"0.png"],
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"],
[UIImage imageNamed:@"10.png"],
[UIImage imageNamed:@"11.png"],
[UIImage imageNamed:@"12.png"],
[UIImage imageNamed:@"13.png"],
[UIImage imageNamed:@"14.png"],
[UIImage imageNamed:@"15.png"],
[UIImage imageNamed:@"16.png"],
[UIImage imageNamed:@"17.png"],
[UIImage imageNamed:@"18.png"],
[UIImage imageNamed:@"19.png"],
[UIImage imageNamed:@"20.png"],
[UIImage imageNamed:@"21.png"],
[UIImage imageNamed:@"22.png"],
[UIImage imageNamed:@"23.png"],
[UIImage imageNamed:@"24.png"],
[UIImage imageNamed:@"25.png"],
[UIImage imageNamed:@"26.png"],
[UIImage imageNamed:@"27.png"],
[UIImage imageNamed:@"28.png"],
[UIImage imageNamed:@"29.png"],
[UIImage imageNamed:@"30.png"],
[UIImage imageNamed:@"31.png"],nil];
int randNum = [(NSNumber *)[tim userInfo] intValue];
CGContextRef imageContext = UIGraphicsGetCurrentContext();
UIImage *imgToDisplay = [wisdom objectAtIndex:randNum];
wisdomView.image = imgToDisplay;
По-прежнему получает метод '-objectAtIndex:' предупреждение не найдено, и теперь '' может не отвечать на ''. NSArray может не отвечать на -objectAtIndex. Arrrrgggg.
Спасибо за ваше время.