我需要访问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中显示图像。
很难想出这个问题。非常感谢所有帮助。谢谢你的时间。
这是我在Josh的帮助下实施的当前代码,截至2010年7月6日晚11点13分。我想我几乎拥有它......但我正在做的事情不起作用。我一直收到警告“方法”-objectAtIndex not found(返回类型默认为'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];
}
我很感激帮助。谢谢你的时间。
这是改变后的代码仍然不起作用....我不理解某些东西。 2011年7月7日下午1:19
-(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。
感谢您的时间。
答案 0 :(得分:2)
将生成的号码塞进计时器的user info位:
[NSTimer scheduledTimerWithTimeInterval:0
target:self
selector:@selector(seeImage:)
userInfo:[NSNumber numberWithInt:randNum]
repeats:NO];
然后在计时器的方法中将其恢复。顺便说一句,计时器方法应该有一个参数。*计时器将自己传递给方法就是这样的情况,即,当你需要发送一些信息以便在它发生时使用。
- (void) seeImage: (NSTimer *)tim {
// Retrieve the NSNumber, using a cast because userInfo returns id
int randNum = [(NSNumber *)[tim userInfo] intValue];
NSImage * imgToDisplay = [arrayOfImgs objectAtIndex:randNum];
// and so on...
}
*虽然它们可以使用任意数量的参数,包括0。
答案 1 :(得分:1)
尝试:
[imageArray objectAtIndex:randNum]
编辑:根据评论中的其他信息,我认为@Josh Caswell的回答更能回答这个问题。
答案 2 :(得分:1)
//创建随机数,1 - 32 randNum = arc4random()%32;
答案 3 :(得分:0)
我修好了!谢谢Josh的帮助!
-(IBAction) seeImage
{
int randNum;
//when user clicks the button
//create random number, 0 - 31
randNum = arc4random() %32;
NSString *randValText = [[NSString alloc]initWithFormat:@"%d", randNum];
//Output random number to label - testing code
//labelRandText.text = randValText;
///set image opacity to 0
wisdomView.alpha = 0.0;
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];
CGContextRef imageContext = UIGraphicsGetCurrentContext();
//pick image randNum index position
wisdomView.image = [wisdom objectAtIndex:randNum];
wisdomView.alpha = 0.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];
[randValText release];
}
感谢您的时间!