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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:11:13+00:00 2026-06-02T11:11:13+00:00

I have a grid of UIButtons. When I hit an ‘edit’ button, I want

  • 0

I have a grid of UIButtons. When I hit an ‘edit’ button, I want a delete button to appear over each of these buttons, which when pressed, deletes the button (and associated data). A bit like apple’s home screen, when you hold down a button and it starts to wiggle with an X in the corner.

According to this post: Subclass UIButton to add a property I can use Associative References to add a property to each of my buttons. I’ve tried to add a UIButton as a property of my custom UIButton but I can’t seem to get it to appear and have the feeling this isn’t the right way to go. Here’s my custom button main:

    #import "UIButton+Property.h"
#import <objc/runtime.h>

@implementation UIButton(Property)

static char UIB_DELETEBUTTON_KEY;

@dynamic deleteButton;


- (void)setDeleteButton:(UIButton *)deleteButton {
    deleteButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
    deleteButton.frame = CGRectMake(100, 100, 50, 50);
    objc_setAssociatedObject(self, &UIB_DELETEBUTTON_KEY, deleteButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIButton *)deleteButton {
    return (UIButton *)objc_getAssociatedObject(self, &UIB_DELETEBUTTON_KEY);
}

@end

And here’s where I add the buttons programmatically:

//Create a custom button for each custom book doc
for (int i = 0; i < [customBookDocs count]; ++i) {
    BookDoc *customBookDoc = [customBookDocs objectAtIndex:i];
    NSString *bookTitle = customBookDoc.book.title;

    //create a button for each book
    CGRect frame = CGRectMake(xCoord, yCoord, 200, 200);
    UIButton *bookButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    bookButton.bookDoc = customBookDoc;
    [bookButton setFrame:frame];
    [bookButton setTitle:bookTitle forState:UIControlStateNormal];
    [bookButton addTarget:self action:@selector(bookButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    xCoord += 250;

    [self.view addSubview:bookButton];
    [self.view addSubview:bookButton.deleteButton];
}

Is there an easier more sensible way to do this? Or am I on the right track?

  • 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-02T11:11:15+00:00Added an answer on June 2, 2026 at 11:11 am

    ORIGINAL RESPONSE BEGAN:

    … Someone else may have more to say about that, but I’m not sure why you’d need to use object association here. You can certainly add another button to your button as a property using regular subclassing, which is the route that I would take. …

    EDITS BELOW:

    I thought that I had subclassed a UI control directly, but I realized that I was mistaken when I went to look for the code. @Joe rightly pointed out in the comments that there are issues with directly subclassing UI controls.

    I was able to implement something like the functionality you described without using Associated Objects, by creating a wrapper class to hold the button and its related delete button. It works, but it’s not very flexible, so I would generally recommend @Joe’s method as a better solution.

    Here’s the relevant code:

    I threw all of the code into the appDelegate to keep it simple. I don’t recommend that in real life.

    AppDelegate.m:

    @implementation AppDelegate
    
    @synthesize window = _window;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
        self.window.backgroundColor = [UIColor whiteColor];
    
        UIButton *toggleDeleteButtons = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [toggleDeleteButtons setFrame:CGRectMake(20, 45, 280, 45)];
        [toggleDeleteButtons setTitle:@"Toggle Delete" forState:UIControlStateNormal];
        [toggleDeleteButtons addTarget:self action:@selector(toggleDeleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [[self window] addSubview:toggleDeleteButtons];
    
        ButtonWrapper *myButtonWrapper = [[ButtonWrapper alloc] init];
        [[myButtonWrapper button] setFrame:CGRectMake(20, 100, 200, 45)];
        [[myButtonWrapper button] setTitle:@"This is my button" forState:UIControlStateNormal];
        [[myButtonWrapper deleteButton] addTarget:self action:@selector(buttonDeleteRequested:) forControlEvents:UIControlEventTouchUpInside];
        [[myButtonWrapper deleteButton] setTag:0];
        [[self window] addSubview:[myButtonWrapper button]];
        buttonWrapper1 = myButtonWrapper;
    
        // Added instance called anotherButtonWrapper with tag 1, as above
    
        // Added instance called stillAnotherButtonWrapper with tag 2, as above
    
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    - (void)toggleDeleteButtonAction {
        static BOOL deleteButtonsShown;
    
        [buttonWrapper1 showDeleteButton:!deleteButtonsShown];
        [buttonWrapper2 showDeleteButton:!deleteButtonsShown];
        [buttonWrapper3 showDeleteButton:!deleteButtonsShown];
        deleteButtonsShown = !deleteButtonsShown;
    }
    
    - (void)buttonDeleteRequested:(UIButton *)deleteButton {
        // delete the specified button here
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete" message:[NSString stringWithFormat:@"Delete was pressed on button %i",[deleteButton tag]]delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    

    ButtonWrapper.m:

    @implementation ButtonWrapper
    
    @synthesize button;
    @synthesize deleteButton;
    
    - (ButtonWrapper *)init {
        ButtonWrapper *newWrapper = [ButtonWrapper alloc];
    
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [myButton setFrame:CGRectZero];
    
        UIButton *myDeleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [myDeleteButton setFrame:CGRectMake(0, 0, 100, 40)];
        [myDeleteButton setTitle:@"Delete" forState:UIControlStateNormal];
        [myDeleteButton setHidden:TRUE];
        [myButton addSubview:myDeleteButton];
    
        [newWrapper setButton:myButton];
        [newWrapper setDeleteButton:myDeleteButton];
    
        return newWrapper;
    }
    
    - (void)showDeleteButton:(BOOL)showButton {
        if (showButton) {
            [[self deleteButton] setHidden:FALSE];
            [[self deleteButton] setEnabled:TRUE];    }
        else {
            [[self deleteButton] setHidden:TRUE];
            [[self deleteButton] setEnabled:FALSE];
        }
    }
    @end
    

    This solution did not require me to implement all of the UI properties, but it did require extra work to hook up the embedded delegates, which is cumbersome. There may be a way to pass the delegates into the wrapper at initialization, but I couldn’t make it work.

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

Sidebar

Related Questions

I'm going to have a 10x10 grid of UIButton objects. Each of these UIButtons
I have grid which displays users information. I have commandColumn with edit, delete commands.
I have grid of button. I want to a button to be clicked(invoke click
I have grid view and now i want to set specific width for each
I have grid view which contains five radio buttons per row. Out of these
I have grid of images, when the mouse is over any given image a
i have data-grid in my application and i want to customize my data-grid so
I have a grid with some controls inside. I want to hide the grid
I have Grid with 10 rows and 10 column, and I want in code
in my web application i have grid view control with edit property true. Now

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.