我正在围绕实现这一目标的最佳方式展开,所以也许你可以帮助我:
当UIButton响应选择器highlightImage
时,如何设置所需的challengeId =我appDelegate.availableArray
的索引?
在我的cellForRowAtIndexPath
方法中,我正在使用按钮创建UIScrollView
:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
[scrollView setContentSize:CGSizeMake(500, 78)];
[self createScrollButtons];
[[cell contentView] addSubview:scrollView];
我的createScrollButtons
循环并在scrollView
中创建按钮,循环显示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 in
availableArray`,沿着scrollView正确定位它们。
- (void)createButtonAtX:(int) x AndY:(int) y withChallenge:(id)challengeId
{
CGRect buttonRect = CGRectMake(x, y, 80, 78);
ChallengeButton *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
来确定按下哪个按钮。 (理想情况下,该按钮所关联的availableArray
元素)...我已经对UIButton进行了子类化并添加了int
值,以便能够调用[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;
}
}
我的问题是我无法弄清楚如何将appDelegate的availableArray
元素放入我的highlightImage中,因为UIButton的[capeButton addTarget:challengeId action:@selector(highlightImage:) forControlEvents:UIControlEventTouchUpInside]
将不允许我从流程中的循环中传递challengeId。
我能够将任何整数值传递给setChallengeId,并按预期处理。我只需要从循环中获取appDelegate.availableArray
id
值。
任何方向都会很棒!:)
答案 0 :(得分:1)
您将availableArray
转换为highlightImage:
的方式与createScrollButtons
相同:
SuperHeroGameAppDelegate *appDelegate = (SuperHeroGameAppDelegate *)[[UIApplication sharedApplication] delegate];
然后访问appDelegate.availableArray
。
我认为这不是你真正要问的问题。我想你想要的是在setChallengeId:
创建它时在每个ChallengeButton上调用createButtonAtX:AndY:withChallenge:
。然后在highlightImage:
执行selectedChallenge = [sender challengeId]
而不是[sender setChallengeId:selectedChallenge]
。