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 8254293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:08:21+00:00 2026-06-08T01:08:21+00:00

I have problem accessing UIButton after adding it to as a subview. When i

  • 0

I have problem accessing UIButton after adding it to as a subview. When i try to adjust the buttons location on the view I always get a UIView instead of UIButton only with the first button that i added. The rest return as UIButton when i use isKindOfClass. Here is a code snippet as follow

First i call createTiles method from viewDidload to add the buttons as a subviews then adjust the buttons lay out after detecting device orientation by calling adustLandscapeTiles/Portrait method.

I’m not sure why that is happening any idea ? ?

// Adding buttons to view 
- (void)createTiles
{    
   for(int i=0;i<[self.contentList count];i++)
  {
       UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       [button addTarget:self action:@selector(buttonSelection:)forControlEvents:UIControlEventTouchDown];
       [button setTitle:[ [contentList objectAtIndex:i] valueForKey:@"nameKey"]  forState:UIControlStateNormal];
        button.tag=i;

        [self.view insertSubview:button atIndex:0];
        NSLog(@"adding %i %@",i , [ [contentList objectAtIndex:i] valueForKey:@"nameKey"]);
        [button release];

   }
  }

- (void)adustLandscapeTiles
{
    for (int row = 0; row < Portrait_TILE_ROWS; ++row)
    {
        for (int col = 0; col < Portrait_TILE_COLUMNS; ++col)
        {
            int index = (row * Portrait_TILE_COLUMNS) + col;      
            CGRect frame = CGRectMake(LandScape_TILE_MARGIN + col * (LandScape_TILE_MARGIN + LandScape_TILE_WIDTH),
                                  LandScape_TILE_MARGIN + row * (LandScape_TILE_MARGIN + LandScape_TILE_HEIGHT),
                                  LandScape_TILE_WIDTH, LandScape_TILE_HEIGHT);
  /// check subview is a button      
  NSLog(@"index: %i tag : %i Is of type UIButton?: %@",index,[[self.view viewWithTag:index] tag], ([ [self.view viewWithTag:index] isKindOfClass: [UIButton class]])? @"Yes" : @"No");

        /// Only the first button added with tag zero return UView instead of UIButton ??
            UIButton *tmb= (UIButton *)[self.view viewWithTag:index];
           if([tmb isKindOfClass:[UIButton class]]) //isKindOfClass
            {   
                [  [self.view viewWithTag:index] setFrame:frame];

            }

        }
    }

}
  • 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-08T01:08:23+00:00Added an answer on June 8, 2026 at 1:08 am

    While I was adding a more robust solution to your problem I noticed you are over-releasing button. You should not call [button release] you do not own it! But that is not your only problem…

    If you do not assign a tag to a UIView it will have a tag of zero, so if your view has some other subviews that you did not assign tags to you may be getting one of them. Try adding some offset to your tags, for example:

    // Somewhere early in your .m file
    const NSInteger kButtonTagOffset 256
    
    // In your createTiles
    button.tag = (kButtonTagOffset + i);
    

    Obviously you need to add this offset when you retrieve your tiles too.

    Having said this, your current method is what a more experienced programmer might call fragile if someone ever subclasses your class or even if you edit sometime later you may assign conflicting tags and cause some problems. You might consider a more robust solution.

    Since what you really want to do is maintain as array of buttons that you can access by index, why not do exactly that without relying on tags?

    So in your UIViewController add a NSMutableArray to hold your buttons.

    // In your .h or with private methods:
    @property (nonatomic, retain) NSMutableArray* buttons;
    
    // At the top of your @implementation
    @synthesize buttons;
    
    // A modified createTiles 
    // Adding buttons to view 
    - (void)createTiles
    {     
        self.buttons = nil; /* in case viewDidUnload id not do this. */
        self.buttons = [[[NSMutableArray alloc] init] autorelease]; 
        for(int i=0;i<[self.contentList count];i++)
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [button addTarget:self    
                       action:@selector(buttonSelection:)
             forControlEvents:UIControlEventTouchDown];
            [button setTitle:[[contentList objectAtIndex:i] valueForKey:@"nameKey"]  
                    forState:UIControlStateNormal];
    
            [self.view insertSubview:button atIndex:0];
            NSLog(@"adding %i %@", i, 
                            [[contentList objectAtIndex:i] valueForKey:@"nameKey"]);
            [self.buttons insertObject:button atIndex:0];
        }
    }
    
    - (void)adustLandscapeTiles
    {
        for (int row = 0; row < Portrait_TILE_ROWS; ++row)
        {
            for (int col = 0; col < Portrait_TILE_COLUMNS; ++col)
            {
                int index = (row * Portrait_TILE_COLUMNS) + col;    
                UIView* buttonView = [self.buttons objectAtIndex:index];
    
                CGRect frame = CGRectMake(LandScape_TILE_MARGIN + col *    
                                     (LandScape_TILE_MARGIN + LandScape_TILE_WIDTH),
                                   LandScape_TILE_MARGIN + row *    
                                     (LandScape_TILE_MARGIN + LandScape_TILE_HEIGHT),
                                   LandScape_TILE_WIDTH, 
                                   LandScape_TILE_HEIGHT);
                // check subview is a button      
                NSLog(@"index: %i Is of type UIButton?: %@", index, 
                        ([ [buttonView viewWithTag:index] isKindOfClass: [UIButton class]])?
                            @"Yes" : @"No");
    
                UIButton *tmb= (UIButton *)buttonView;
                if([tmb isKindOfClass:[UIButton class]]) //isKindOfClass
                {   
                    [buttonView setFrame:frame];
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to get to grips with AJAX, and have a problem accessing
I have a problem accessing JSON data. I'm new to JSON and jquery so
i have problem with libhid . i found that there 2 way 4 accessing
I am having a problem accessing the ViewData object through javascript. I have set
I have a problem with: NHibernate.Cfg.Configuration.SetProperties() Not accepting the IDictionary: NHibernateConfigHandler I get the
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with accessing Facebook graph. I am using a Greasemonkey script. When
I'm quite new to JS and jQuery. I have a problem accessing the properties
I have have a problem loading and accessing data from a value object in
I have a problem wit the software I'm working on. We are accessing Windows

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.