1

私はボタンのセットを持っています。クリックするとボタンのタイトルを表示し、もう一度クリックすると非表示にします。これが私のコードです。

 - (IBAction)buttonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
int index = button.tag;
[temp replaceObjectAtIndex:index withObject:@"1"];
[self showing];
}


 -(void)showing
 {
 UIButton *button = nil;
 NSString *name = nil;
 int i = 0;
 for(UIView *view in self.view.subviews)
 {
    if([view isKindOfClass:[UIButton class]])
    {
        button= (UIButton *)view;
        if(button.tag >= 1 && button.tag <= 16)
        {
            name = [NSString stringWithFormat:@"%@",[texts objectAtIndex:i]];
            if ([[temp objectAtIndex:i] isEqualToString:@"1"])
            {
                [button setTitle:name forState:UIControlStateNormal];
                NSLog(@"current  name :%@",name);
            }
            else
            {
                [button setTitle:@"" forState:UIControlStateNormal];
            }
            i++;
        }
    }
 }
 }

ただし、最初のクリック自体でボタンのタイトル全体が表示されます。クリックしたボタンのタイトルのみを表示したいのですが、コードにどのような変更を加える必要がありますか?

4

3 に答える 3

2
- (IBAction)buttonAction:(id)sender
{    
    UIButton *button = (UIButton *)sender;
    if (![button.titleLabel.text isEqualToString:@""]) {

        [button setTitle:@"" forState:UIControlStateNormal];
    }
    else {
         // set title same like as you set in showing method
        [button setTitle:GiveTitleHere forState:UIControlStateNormal];        
    }
}
于 2012-11-05T10:29:05.020 に答える
1

tempビューコントローラーでクラスレベル変数である配列を設定したと仮定しています。textArrayのような適切な名前を付けることをお勧めします

したがって、次のことを試してください。

-(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; //this is because the array index start from 0, 
                            //but tags have to be more than 0 to be valid
    NSArray* subViewArray = [self.view subviews];

    NSInteger i = 0;

    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
                self.previousButtonTag = i; //make a class variable

            }
            else {
                text = @"";
            }
                //NEW EDIT 

                /*  1.fetch the title from self.textArray for "self.previousButtonTag"  
                    2.the title for the current button is there in "text" variable
                    3.compare both the strings and do things as per your liking
                */

            i++;
            [btn setTitle:text forState:UIControlStateNormal];
        }// end of IF
    } //end of FOR
} //end of METHOD
于 2012-11-05T12:02:44.857 に答える
0

設定button.titlelabel.text=@"some string"すると、ボタンをクリックすると自動的に消えます

于 2012-11-05T10:28:30.007 に答える