开发者

Trouble accessing objects in NSArray using randNum

开发者 https://www.devze.com 2023-03-18 06:51 出处:网络
I need to access an NSArray and get a value from it with: -(IBAction) randClicked:(id)sender { int randNum;

I need to access an NSArray and get a value from it with:

-(IBAction) randClicked:(id)sender
{
int randNum;

//when user clicks the button
//create random number, 1 - 32
randNum = arc4random() %32; 

As you see, I can display my random number here:

///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];
}

But now, I need to compare the value of randNum and access an NSArray of images for it's position in the array so I can display an image in an UIImageView.

Having a difficult time trying to figure this one out. All help is greatly appreciated. Thank you for your time.

Here is the current code that I have implemented with help from Josh as of 11:13pm 7/6/2001. I think I almost have it...but something I'm doing is not working. I keep getting the warning "Method '-objectAtIndex not found (return type defaults to '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];

}

I appreciate the help. Thank you for your time.

Here is the changed code that still doesn't work....I'm not understanding something. 1:19PM 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"],
                   开发者_StackOverflow中文版             [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;

Still getting the Method '-objectAtIndex:' not found warning and now '' may not respond to '' 'NSArray' may not respond to '-objectAtIndex'. Arrrrgggg.

Thank you for your time.


Tuck the generated number into the timer's user info slot:

[NSTimer scheduledTimerWithTimeInterval:0 
                                 target:self 
                               selector:@selector(seeImage:)
                               userInfo:[NSNumber numberWithInt:randNum]
                                repeats:NO];

and then get it back out in the timer's method. By the way, timer methods are supposed to have one parameter.* The timer passes itself to the method for just such a case as this, i.e., when you need to send some info along to be used when it fires.

- (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...
}

*Although they do work with any number of parameters, including 0.


Try:

[imageArray objectAtIndex:randNum]

EDIT: based on the additional info in the comments, I think @Josh Caswell's response better answers the question.


//create random number, 1 - 32 randNum = arc4random() %32;

  • that would give you 0 - 31. check your array bounds.


I fixed it! Thanks Josh for the assist!

-(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];

}

Thank you for your time!

0

精彩评论

暂无评论...
验证码 换一张
取 消