0

Im having four buttons in my application,im getting the button title from array ,I want to change the 4 button titles whenever i click any of the button,But here the clicked button title only changed ,here my code,

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

Updated

-(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];

        }
    }
}

}

I connect this method with 4 buttons,i want to change all button title whenever i click any of the buttons,please help to code

2
  • you need to save all these uibuttons in array then u can change the title of all these at click event. Commented Oct 24, 2012 at 10:01
  • @SHAZAD can yu explain via code? Commented Oct 24, 2012 at 10:02

1 Answer 1

2

Because sender holds only that UIButton on which you have clicked.
For changing all UIButton's title you need a loop.
Set tag to all 4 buttons.

Then do this -

-(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];
            }
        }
     }  
}
Sign up to request clarification or add additional context in comments.

9 Comments

if am having another image array how can i show the image names as title in uibuttons
Make image names array then you can get them from array.
My doubt is how to compare if am selecting a button which contains the same image in uiimage view?
I'm not getting you correctly.You have one method of all buttons click ... then how're you doing with your imageView ?
am asking if im putting an image contained array and if showing the titles of button as image name...How could i proceed for tat?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.