1

在这里我的应用程序有一组按钮 12 按钮,按钮标题默认隐藏。按钮标题在单击时显示,

-(IBAction)buttonAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    int index = button.tag;
    [self showing:index]; //now, this method accepts a parameter.
}

-(void)showing:(NSInteger)index
{
UIButton* btn = nil;
index = index - 1;
NSArray* subViewArray = [self.view subviews];

NSInteger i = 0;

for (UIView *view in subViewArray)
{
    if ([view isKindOfClass:[UIButton class]])
    {
        btn = (UIButton*) view;
        if(btn.tag >= 1 && btn.tag <= 16)
        {
            NSString* text;
            UIButton *prevButton = (UIButton*) [self.view viewWithTag:previousButtonTag];
            if (i == index) {

                if ([btn.titleLabel.text isEqualToString:prevButton.titleLabel.text])
                {

                }
                else
                {
                    text=@"";
                }
            text = [texts objectAtIndex:i]; //put the array that you are using
        }
        else {
            text = @"";
        }

        i++;

        [btn setTitle:text forState:UIControlStateNormal];
        }
    }// end of IF
} //end of FOR

}

1.我想比较两个按钮标题值来检查它是否相等。我面临这个问题,

2.仅按按钮标题可见,当我单击其他按钮时,上一个标题被隐藏,我想在单击时启用两个按钮标题,但错误应该隐藏。请建议我如何解决此问题,请执行必要的

4

3 回答 3

0

使用此代码进行一些更改,我只是尝试了解您的要求并为上一个按钮和当前按钮标题的显示标题添加逻辑..

    -(void) showing:(NSInteger)index
    {
        UIButton* btn = nil;
        index = index - 1; 
        NSArray* subViewArray = [self.view subviews];
        NSInteger i = 0;

        UIButton *btnCurTemp = (UIButton *)[self.view viewWithTag:index];


        for (UIView *view in subViewArray) 
        {
            if ([view isKindOfClass:[UIButton class]]) 
            {
                btn = (UIButton*) view;
                NSString* text;
                if (i == index) {
                    text = [self.textArray objectAtIndex:i]; //put the array that you are using
                    [btnCurTemp setTitle:text forState:UIControlStateNormal];
                    UIButton *btnPrevTemp = (UIButton *)[self.view viewWithTag:self.prevButtonTag];

                  if ([btnCurTemp.titleLabel.text isEqualToString:btnPrevTemp.titleLabel.text]) {
                     /// visible your both button title..
                 }
                 else {
                     //// hide button title..
                 }
                    self.previousButtonTag = i; //make a class variable

                }else {
                    text = @"";
                }
                i++;

            }
        } 
    } 
于 2012-11-07T05:24:36.190 回答
0

改变你的showing喜欢:

-(void) showing:(NSInteger)index
{
  UIButton *btn = (UIButton*) [self.view viewWithTag:index];
  NSString *text = [self.textArray objectAtIndex:index]; //put the array that you are using
  [btn setTitle:text forState:UIControlStateNormal];
  if (self.previousButtonTag != -1)
  {
     UIButton *prevButton = (UIButton*) [self.view viewWithTag:previousButtonTag];
     if ([btn.titleLabel.text isEqualToString:prevButton.titleLabel.text])
     {
     }
     else
     {
       //hide the prevButton
       [prevButton setTitle:@"" forState:UIControlStateNormal];
     }
   }
  self.previousButtonTag = index; //make a class variable
}

IButton *btn = (UIButton*) [self.view viewWithTag:index];会给你当前点击的按钮。UIButton *prevButton = (UIButton*) [self.view viewWithTag:previousButtonTag];给你之前点击的按钮。根据您的需要,您可以检查 if 条件中的标题,如果它们相等,则在那里执行您的操作,如果不相等则隐藏上一个按钮标题。

您需要在您的viewDidLoad

于 2012-11-07T04:48:21.227 回答
0

为什么不简单地使用按钮标签作为索引比较数组字符串值 -

NSString * firstString = [self.textArray objectAtIndex:btn.tag];
NSString * secondString = [self.textArray objectAtIndex:prevButton.tag];
if([firstString isEquleToString:secondString]){

}else{

} 
于 2012-11-07T05:26:12.487 回答