user2963228 user2963228 - 19 days ago 6
iOS Question

Unable to replace the tags of UIButton(s)

I have say for example 7

UIButtons
namely c1 to c7. Now I have assigned tags starting from 1 to 7 respectively for the
UIButtons
c1 to c7.

Now when I select c2 for example it is removed from
superView
so now the tag for c2 which was 2 is transferred to c3 , 3 to c4 and so on.

This is what I have tried but logic is not working properly. I have posted question with similar concern before but didn't get any proper response.

-(void)totesttheFunction
{
for(int i=0; i<7; i++)
{
UIButton *testHere = (UIButton*)[self.view viewWithTag:i];
if([testHere isSelected])
{
int backuptagFor = testHere.tag;
CGFloat diff = 30.0;
for(int j=i+1; j<7;j++)
{
UIButton *btnToReplace = (UIButton*)[self.view viewWithTag:j];
CGRect setRect = CGRectMake(btnToReplace.frame.origin.x-diff, btnToReplace.frame.origin.y, btnToReplace.frame.size.width, btnToReplace.frame.size.height);
btnToReplace.tag = backuptagFor;
[testHere removeFromSuperview];

}
}
}
}

Answer
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    for(int itemIndex = 1; itemIndex <= 7; itemIndex++)
    {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(itemIndex*40, 10, 30, 30)];
        btn.tag = itemIndex;
        [self.view addSubview:btn];

        if (itemIndex == 3 ||itemIndex == 4)
        {
            [btn setSelected:YES];
        }
    }

    [self testFunction];
}


- (void)testFunction
{
    int totalButtons = 7;
    int totalRemovedButtons = 0;

    for(int itemIndex = 1; itemIndex <= totalButtons; itemIndex++)
    {
        UIButton *testHere = ([[self.view viewWithTag:itemIndex] isKindOfClass:[UIButton class]])?(UIButton *)[self.view viewWithTag:itemIndex]:nil;
        if([testHere isSelected])
        {
            [testHere removeFromSuperview];
            NSLog(@"removed button with tag:%d",itemIndex + totalRemovedButtons);

            for (int tempItemIndx = itemIndex + 1; tempItemIndx <= totalButtons; tempItemIndx++)
            {
                 UIButton *nextButton = ([[self.view viewWithTag:tempItemIndx] isKindOfClass:[UIButton class]])?(UIButton *)[self.view viewWithTag:tempItemIndx]:nil;
                nextButton.tag = tempItemIndx - 1;
            }

           itemIndex--;
           totalRemovedButtons ++;
        }

        NSLog(@"loop run %d",itemIndex);
    }

    NSLog(@"-------------------------------------------------------------------");

    //Checking the updated tags.

    for(int itemIndex = 1; itemIndex <= (totalButtons - totalRemovedButtons); itemIndex++)
    {
        UIButton *testHere = ([[self.view viewWithTag:itemIndex] isKindOfClass:[UIButton class]])?(UIButton *)[self.view viewWithTag:itemIndex]:nil;
        NSLog(@"New tags %d",testHere.tag);
    }
}

Output:

 loop run 1
 loop run 2
 removed button with tag:3
 loop run 2
 removed button with tag:4
 loop run 2
 loop run 3
 loop run 4
 loop run 5
 loop run 6
 loop run 7
 ---------------------
 New tags 1
 New tags 2
 New tags 3
 New tags 4
 New tags 5