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

  • Home
  • SEARCH
  • 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 7493355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:54:31+00:00 2026-05-29T16:54:31+00:00

I have a UIView *buttonView filled with buttons. I need to update my list

  • 0

I have a UIView *buttonView filled with buttons. I need to update my list and re-populate my buttonView. I implemented:

if ([[self.buttonView.subviews objectAtIndex:i] isKindOfClass:[UIButon class]]
    [[self.buttonView.subviews objectAtIndex:i] removeFromSuperview];

but it failed, it would not remove all the buttons (with my test with 8 buttons, it removed every other button :-?)

I then tried:

for(UIView *subview in self.buttonView.subviews) 
{
    if([subview isKindOfClass:[UIButton class]]) 
        [subview removeFromSuperview];
}

and it worked perfectly.

Shouldn’t both loops accomplish the same thing?

I’m guessing there is something I don’t know about fast enumeration that would explain this?

  • 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-05-29T16:54:32+00:00Added an answer on May 29, 2026 at 4:54 pm

    I don’t believe you’re allowed to modify the collection you’re enumerating, so be careful with that. It seems like it handles the changes just fine though, and it will get through every item (unlike the for loop).

    In the first case, it wouldn’t work because once you remove index 0, something else becomes index 0, but you’ve moved on to index 1. You could fix it by decrementing your loop variable in the event that the remove succeeds, so that the loop increments it to its previous value:

    if ([[self.buttonView.subviews objectAtIndex:i] isKindOfClass:[UIButton class]]) {
        [[self.buttonView.subviews objectAtIndex:i] removeFromSuperview];
        --i;
    }
    

    What happens with your for loop:

    1,2,3,4,5,6,7,8 and remove first item (1)

    2,3,4,5,6,7,8 and remove second item (3)

    2,4,5,6,7,8 and remove third item (5)

    2,4,6,7,8 and remove fourth item (7).

    2,4,6,8 and you’re on index 5, which is greater than length, so you’re done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIView that has an image and some buttons as its subviews.
I have a UIView that has several UIImageViews as subviews. Each of these subviews
I have a UIView and there are 5 subViews on it with index 0,1,2,3,4,5
I have a UIView which contains some controls (e.g. buttons, labels, etc). I overlay
I have a UIView. I need to make only it's top left and top
I have a UIView with 4 buttons on it and another UIView on top
I have a UIView subclass that programmatically creates and adds a number of subviews.
I have a UIview which I am drawing a series of buttons on to.
I have an UIView with rounded corners and drop shadow, implemented and working. But
I have a UIView and I'm trying to set its layer properties. self.colorSwatch =

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.