2010-08-27 108 views
0

我正在做一个多选题(MCQ)的应用程序,我必须从web服务中获得问题和答案。我有4个选项1个问题,在4个选项中,我有1个正确的答案。每个选项都以编程方式创建了一个按钮。这是我做的:如何找到哪个编程创建的按钮被点击?

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(40, yButton, 30, 30); 
    [button setTitle:@" " forState:UIControlStateNormal]; 

[button addTarget:self action:@selector(correctPressed) 
forControlEvents:UIControlEventTouchDown]; 
    [buttonArray addObject:button]; 

我在问题中有多少个选项将决定将创建多少个按钮。现在,当我想显示某个按钮被点击时,该按钮需要保持突出显示,直到我决定更改我的答案。我做了几个方法。

我用IndexPath,希望我可以使用indexPath.rowTableView一样。它根本没有工作。谁能帮忙?

感谢

回答

0
for(int i=0; i<[videolistArray count];i++){ 


    NSArray *myWords = [[[videolistArray objectAtIndex:i] valueForKey:@"video"] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; 
    //NSLog(@"%@",[myWords lastObject]); 

    UIButton *guitaristButton = [[UIButton alloc]init]; 
    guitaristButton.frame = CGRectMake(0, ycomp, 300, 35); 
    guitaristButton.tag = i; 
    [guitaristButton setBackgroundImage:[UIImage imageNamed:@"green-buttons.png"] forState:UIControlStateNormal]; 
    [guitaristButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    [guitaristButton setTitle:[myWords lastObject] forState:UIControlStateNormal]; 
    [guitaristButton addTarget:self action:@selector(VideoButtonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    //[guitaristButton performSelector:<#(SEL)aSelector#> withObject:<#(id)object1#> withObject:<#(id)object2#> 
    [videoScrollView addSubview:guitaristButton]; 
    [guitaristButton release]; 

    ycomp = ycomp+35; 
} 
videoScrollView.contentSize=CGSizeMake(300, ycomp); 

} - (无效)VideoButtonSelected:(的UIButton *)发件人{ 的NSArray * myWords = [[[videolistArray objectAtIndex:sender.tag] valueForKey:@ “视频”] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@ “/”]]; // NSLog(@“%@”,[myWords lastObject]); [setAlarmVedioButton setTitle:[myWords lastObject] forState:UIControlStateNormal]; [videoNameButtonView removeFromSuperview]; vedioString = [[videolistArray objectAtIndex:sender.tag] valueForKey:@“video”]; }

在上面的代码中我做的UIButton与阵列的帮助下计数,当我选择任何按钮,然后它最后一个按钮

1

更改为

[button addTarget:self action:@selector(correctPressed) 
forControlEvents:UIControlEventTouchDown]; 
    [buttonArray addObject:button]; 

[button addTarget:self action:@selector(correctPressed:) 
forControlEvents:UIControlEventTouchDown]; 
    [buttonArray addObject:button]; 

现在在你的选择,你会得到该按钮的引用。

例如。

UIButton * lastButtonPressed = nil; 
-(void) correctPressed:(UIButton *) theButton{ 
    [theButton setHighlighted:YES]; 
    if(lastButtonPressed) [lastButtonPressed setHighlighted:NO]; 
    lastButtonPressed = theButton; 

} 

**您可能想要使用所选按钮的方法。所以你可以设置一个按钮来代替。

我只是输入了这段代码,检查了我的@selector变化,那个setHighlighted实际上是正确的,但你应该明白了。

现在你的代码会调用选择器,它应该有一个对调用按钮的引用。

John。

+0

感谢约翰显示阵列的相同文本值,将尝试代码出来。你能向我解释为什么你使用'(UIButton *)theButton'作为? – 2010-08-27 05:47:36

+0

编辑:我已经试过了代码,它的工作原理,但不是我想要的方式。我希望它被突出显示,直到我再次点击其他按钮或其自身。 '[theButton setHighlighted:YES]'当我松开手指时会变成'NO'。 – 2010-08-27 05:59:34

+0

我觉得我很烦你,对不起约翰。但它仍然不起作用。对你起作用吗?当您点击按钮时,释放手指时它会保持突出显示状态? – 2010-08-27 06:43:28

0

它没问题,我解决了这个问题。

我所做的是在代码中的某处做了button.tag = number; number++。通过使用标记方法,我可以跟踪arrays单击哪个按钮。

-(void)correctAnswer 
{ 
    yButton += 50; 
    yLabel += 50; 

    label = [[UILabel alloc] initWithFrame:CGRectMake(75, yLabel, 150, 30)]; 
    label.backgroundColor = [UIColor clearColor]; 
    [label setText:Option]; 
    [labelArray addObject:label]; 
    [self addSubview:label]; 

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(40, yButton, 30, 30); 
    button.tag = idx; 
    NSLog(@"-------====== Tag Index is: %i", idx); 
    UIImage * btnImage1 = [UIImage imageNamed:@"uncheckedCheckbox.png"]; 
    [button setImage:btnImage1 forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(correctPressed:) 
    forControlEvents:UIControlEventTouchUpInside]; 

    [buttonArray addObject:button]; 
idx++; 
[self addSubview:button]; 

} 


-(void)correctPressed:(id)sender { 

int but = (int)[(UIButton*)sender tag]; 

if(correctValue) { 
    UIImage * btnImage1 = [UIImage imageNamed:@"uncheckedCheckbox.png"]; 
    UIButton *btn1 = [buttonArray objectAtIndex:but]; 
    [btn1 setImage:btnImage1 forState:UIControlStateNormal]; 
    [btn1 setImage:btnImage1 forState:UIControlStateNormal]; 
    [btn1 setTitle:@"" forState:UIControlStateNormal]; 
    correctValue = NO; 

} else { 
    UIImage * btnImage2 = [UIImage imageNamed:@"checkedCheckbox.png"]; 
    UIButton *btn2 = [buttonArray objectAtIndex:but]; 
    [btn2 setImage:btnImage2 forState:UIControlStateNormal]; 
    [btn2 setImage:btnImage2 forState:UIControlStateNormal]; 
    [btn2 setTitle:@"" forState:UIControlStateNormal]; 
    correctValue = YES; 
} 

NSLog(@"Correct answer!!");  
}