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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:11:07+00:00 2026-05-22T23:11:07+00:00

I am trying to make a program that dynamically creates a button using the

  • 0

I am trying to make a program that dynamically creates a button using the command:

[UIButton buttonWithType:UIButtonTypeRoundedRect]

But when I use these commands the delete the button I create:

[currentButton removeFromSuperview];
[currentButton dealloc];
[currentButton release];

I receive an error. How would I go about removing and deallocating the memory from a uibutton?

  • 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-22T23:11:08+00:00Added an answer on May 22, 2026 at 11:11 pm

    In the Objective-C/Cocoa framework, you encounter two different ways to receive objects: ones which you have explicitly allocated memory for (via a constructor) and ones that you have received memory reference to (via a class method).

    FooBar *fone = [[FooBar alloc] initWithText:@"Hello, World!"];
    

    In this example, memory is explicitly being allocated for the object by your call, using the alloc method, and then it is being initialized with data using the initWithText method that would have a method header like this:

    - (id)initWithText:(NSString *)text;
    

    On the other hand, you also will encounter objects that are created by classes automatically for you. An example of this would be below:

    FooBar *ftwo = [FooBar fooBarWithWelcomeText];
    

    In this example, a FooBar object is being returned even though we are not calling alloc to allocate memory for it. There are many different reasons to implement the method like this, but its mainly done to abstract certain details from the code that is using the object. The above example would have a corresponding method header like this:

    + (FooBar *)fooBarWithWelcomeText;
    

    Depending on which approach is used, it changes how you interact with the memory of the object. So for the first example, after allocating the memory for the object you receive it back with a retain count of 1. If you are done using the object, you need to explictly release it with

    [fone release];
    

    In the second example, you are receiving an autoreleased object, which will be deallocated once the autoreleasepool is drained. If you want to keep it, you must explicitly retain with:

    [ftwo retain];
    

    If you do not wish to retain it, you can just leave it as is and it will be deallocated automatically. You can tell a method uses autorelease by two characteristics: 1) you will not utilize alloc when you receive the object; and 2) there will be a “+” next to the method heading. This means that the method is declared as a class method (similar to Java static methods).

    So to finally answer your specific situation, you only need to make sure that the retain count is lowered to 1 (The only object having a reference to it was the autorelease pool). In your example, it would be done like this:

    UIButton *currentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [someView addSubView:currentButton];
    
    // some code later
    [currentButton removeFromSuperview];
    

    You do not need the release statements because you never explicitly retained it. When you added the UIButton to another view, it retained the object so it incremented the reference count to 2. By removing it from the view, it is lowered back down to 1 so that when the autorelease pool is flushed, your UIButton will be deallocated. Btw, never call the dealloc method directly. When the retain count of an object is decremented to 0, the dealloc method will automatically be called.

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

Sidebar

Related Questions

I am trying to make a simple program that handles files and directories, but
I'm trying to make a program that will draw lines over a picturebox using
I'm trying to make a program in Visual C# that has my one created
Trying to make a make generic select control that I can dynamically add elements
I'm trying to make a program that check the ISBN number and sees if
I'm trying to make a program that read a file line by line and
i'm trying to make a program that inserts data into specific places in existing
I'm just trying to make a program that displays a window with a box
I'm trying to make a program in python which creates a fullscreen window and
I am trying to make a program that shows all the active applications. I

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.