Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:19:05+00:00 2026-06-04T18:19:05+00:00

-(void)setButtons_AsPerTheMatrixSelection { int x = 7; int y = 60; for (int j =

  • 0

-(void)setButtons_AsPerTheMatrixSelection

{

    int x = 7;
    int y = 60;

    for (int j = 0; j <= 35; ++j)
    {
        if (j <= 5)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        else if (j > 5  && j <= 11)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-306, y+51, width, height)];
        else if (j > 11  && j <= 17)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-612, y+102, width, height)];
        else if (j > 17  && j <= 23)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-918, y+153, width, height)];
        else if (j > 23  && j <= 29)    
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1224, y+204, width, height)];
        else if (j > 29  && j <= 35)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1530, y+255, width, height)];

        btnMatrix.tag = j;
        [btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
        btnMatrix.userInteractionEnabled = TRUE;
        [btnMatrix addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];

        [viewWithButtons addSubview:btnMatrix];
        [self.view addSubview:viewWithButtons];

        x = x + 51;
        y = y;
        [arrButton addObject:btnMatrix];
    }

}

This is my code to add dynamic buttons as per the matrix of 6*6

here i am adding the button tag also,

Now i am going to click on button & would like to change background image with this code…

-(void)changeImage:(id)sender

{

UIImage *img = [UIImage imageNamed:@"No.png"];

UIButton *tmpButton = (UIButton *)[self.view viewWithTag:btnMatrix.tag];

NSLog(@"btn.tag is... %d",tmpButton.tag);


[btnMatrix setBackgroundImage:img forState:UIControlStateNormal];

}

When i click to any button it always changing image of last tagged button…

i have to change Background of the selected button itself.

how can it be possible.

Please guide me i am not getting a part where i am doing a mistake.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:19:06+00:00Added an answer on June 4, 2026 at 6:19 pm
    -(void)changeImage:(UIButton*)sender  
    {
        UIImage *img = [UIImage imageNamed:@"No.png"];
        [sender setBackgroundImage:img forState:UIControlStateNormal];
    }
    

    BTW, your code is too messy, you may do like this

    int pw = 306;
    int ph = 51;
    
    int ch = 6;
    int cv = 6;
    
    for ( int i = 0 ; i < cv ; ++i )
    {
        for ( int j = 0 ; j < ch ; ++j )
        {
            UIButton *btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(7+pw*j, 51+ph*i, width, height)] autorelease];
            btnMatrix.tag = i*ch+j;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
void FileManager::CloseFile(File * const file) { for (int i = 0; i < MAX_OPEN_FILES;
-(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
void f(int){} typedef void (*f_ptr)(int); struct Functor{ void operator()(int){} }; struct X{ operator f_ptr(){
void child(int pid){ printf(Child PID:%d\n,pid); exit(0); } void parent(int pid){ printf(Parent PID:%d\n,pid); exit(0); }
void foo (int x) { struct A { static const int d = 0;
void (int a[]) { a[5] = 3; // this is wrong? } Can I
void main() { const int * a; *a = 5; } gcc error :
void max_min(sqlite3 *db) { //call back********* int i, ncols; sqlite3_stmt *stmt; char *sql; const
- (void)viewDidUnload { self.GPSArray = nil; self.accelerometerArray = nil; self.headingArray = nil; self.managedObjectContext =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.