如何使用索引或标记值比较2按钮标题

时间:2012-11-07 04:41:28

标签: iphone ios uibutton

这里我的应用程序我有一组按钮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.仅按下按钮标题仅可见,当我点击其他按钮以前的标题被隐藏时,我想在点击时启用两个按钮标题,如果它是错误的应该被隐藏。请告诉我如何解决这个问题,请做必要的

3 个答案:

答案 0 :(得分: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

中将上一个按钮标记初始化为-1

答案 1 :(得分: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++;

            }
        } 
    } 

答案 2 :(得分:0)

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

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

}else{

}