0

私のアプリケーションには4つのボタンがあり、配列からボタンのタイトルを取得しています。ボタンのいずれかをクリックするたびに4つのボタンのタイトルを変更したいのですが、ここではクリックしたボタンのタイトルのみが変更されました。ここでは私のコードです。

-(IBAction)setting:(id)sender
{
int value = rand() % ([arraytext count] -1) ;
UIButton *mybuton = (UIButton *)sender;
[mybuton setTitle:[arraytext objectAtIndex:value] forState:UIControlStateNormal];
}

更新しました

-(IBAction)answersetting:(id)sender
{

UIButton *mybutton = (UIButton *)sender;
static int j = 0;
if(sender == mybutton)
    j++;
if (j >= arcount)
{
    j = 0;
}
else if(j < 0)
{
    j = arcount - 1;
}

CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction    functionWithName:kCAMediaTimingFunctionEaseOut];
transition.type = kCATruncationMiddle;

[animage.layer addAnimation:transition forKey:nil];    
animage.image=[arimage objectAtIndex:j];

for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UIButton class]])
    {
        UIButton *button = (UIButton *)view;
        if(button.tag == 1||button.tag == 2||button.tag == 3||button.tag == 4)
        {
            int value = rand() % ([artext count] -1) ;
            NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
            [dictionary setValue:animage.image forKey:@"button"];
            [button setTitle:[artext objectAtIndex:value] forState:UIControlStateNormal];

        }
    }
}

}

このメソッドを4つのボタンで接続します。いずれかのボタンをクリックするたびにすべてのボタンのタイトルを変更したいので、コーディングを手伝ってください

4

1 に答える 1

2

クリックしたものsenderだけを保持するためです。 allのタイトルを変更するには、ループが必要です。すべて 4 に 設定します。UIButton
UIButton
tagbuttons

次に、これを行います-

-(IBAction)setting:(id)sender
{
     int value = rand() % ([arraytext count] -1) ;
     for(UIView *view in self.view.subviews)
     {
        if([view isKindOfClass:[UIButton class]])
        {
            UIButton *button = (UIButton *)view;
            if(button.tag == 1||button.tag == 2||button.tag == 3||button.tag == 4)
            {
                [button setTitle:[arraytext objectAtIndex:value] forState:UIControlStateNormal];
            }
        }
     }  
}
于 2012-10-24T10:02:59.743 に答える