开发者

Setting Property on UIButton in Loop

I'm spinning in circles on the best way to implement this, so maybe you can help me out:

How can I make it so when the UIButton responds to the selector highlightImage, it also can set the desired challengeId = the index of my appDelegate.availableArray?

In my cellForRowAtIndexPath method, I'm creating a UIScrollView with buttons:

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
    [scrollView setContentSize:CGSizeMake(500, 78)];

    [self createScrollButtons];

    [[cell contentView] addSubview:scrollView];

My createScrollButtons loops and creates buttons in the scrollView as so, looping over the number of elements in my availableArray

- (void) createScrollButtons
{
    SuperHeroGameAppDelegate *appDelegate = (SuperHeroGameAppDelegate *)[[UIApplication sharedApplication] delegate];

    int x = 0;
    int y = 0;
    NSLog(@"Challenge array = %@",appDelegate.availableArray);
    for (int i = 0; i < [appDelegate.availableArray count]; i++) {
        NSLog(@" ID= %d", [[[appDelegate.availableArray objectAtIndex:i]valueForKey:@"id"] intValue]);
        [self createButtonAtX:x AndY:y withChallenge:[[appDelegate.availableArray objectAtIndex:i]valueForKey:@"id"]];
        x += 90;
    }

}

createButtonAtX andY withChallenge is called for each element inavailableArray`, positioning them correctly along the scrollView.

- (void)createButtonAtX:(int) x AndY:(int) y withChallenge:(id)challengeId
{
    CGRect buttonRect = CGRectMake(x, y, 80, 78); 
    Challeng开发者_运维知识库eButton *capeButton = [ChallengeButton buttonWithType:UIButtonTypeCustom];

    [capeButton setBackgroundColor:[UIColor clearColor]];
    [capeButton setFrame:buttonRect];

    [capeButton addTarget:self action:@selector(highlightImage:) forControlEvents:UIControlEventTouchUpInside];

    UIImage *capeImage = [UIImage imageNamed:@"CapePower.png"];
    [capeButton setBackgroundImage:capeImage forState:UIControlStateNormal];

    [scrollView addSubview: capeButton];
}

highlightImage is called as each buttons selector, to determine which button is pressed. (and ideally with element from the availableArray that button is associated with)... I've subclasses UIButton and added an int value to be able to call [sender setChallengeId:]

- (void) highlightImage:(id)sender
{

    if([sender isSelected] == NO){
        [sender setSelected:YES];

        [sender setChallengeId:selectedChallenge];
        NSLog(@"sender = %d",[sender challengeId]);     
        UIImage *selectedImage = [UIImage imageNamed:@"PowerBarHand.png"];
        [sender setBackgroundImage:selectedImage forState:UIControlStateSelected];

    }
    else {
        [sender setBackgroundImage:[UIImage imageNamed:@"HandstandPower.png"] forState:UIControlStateNormal];   
        [sender setSelected:NO];
        selectedChallenge = 0;
    }
}

My problem is I cannot figure out how to get the appDelegate's availableArray element INTO my highlightImage, as UIButton's [capeButton addTarget:challengeId action:@selector(highlightImage:) forControlEvents:UIControlEventTouchUpInside] will not allow me to pass the challengeId from the loop earlier in the flow.

I am able to pass any integer value to setChallengeId, and it is handling as expected. I just need to get the appDelegate.availableArray id value from the loop.

Any direction would be great!:)


You get availableArray into highlightImage: the same way you got it into createScrollButtons:

SuperHeroGameAppDelegate *appDelegate = (SuperHeroGameAppDelegate *)[[UIApplication sharedApplication] delegate];

and then access appDelegate.availableArray.

I think that's not the question you actually meant to ask, though. I think what you want is to call setChallengeId: on each ChallengeButton as you create it in createButtonAtX:AndY:withChallenge:. Then in highlightImage: do selectedChallenge = [sender challengeId] rather than [sender setChallengeId:selectedChallenge].

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜