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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:58:04+00:00 2026-05-21T09:58:04+00:00

I want to add a uiviewcontroller’s view which has a button and few labels

  • 0

I want to add a uiviewcontroller’s view which has a button and few labels as a content view of a uitableviewcell.

Though i am able to add but the button action causes a crash.

MyViewController *controller = [[MyViewController alloc] initwithnibname:@"MyView" bundle:nil];
[cell.contentview addsubview:controller.view];
[controller release]; // if i comment out this part the button action is received by my view controller.

However there are memory leaks when its removed from view. The dealloc of myviewcontroller is not called.

What is the correct way to do this?

  1. Add a view to a uitableview cell
    which has a button and is handled by
    the viewcontroller

  2. How to assure memory is released
    when the view goes out of scope?

TIA,
Praveen

  • 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-21T09:58:05+00:00Added an answer on May 21, 2026 at 9:58 am

    I think the problem is, that you are releasing the controller and just using the subview which is retained by its superview. The action pattern needs a target which I assume is the released controller. And why should you release your viewController if you only need the view of it? Retain it and keep a reference through a property to it.

    My way of adding subviews to a tableview cell would be in a subclass of UITableViewCell. Let’s assume you are having a subclass of UITableViewCell, say ButtonTableViewCell. The init of the of cell creates and adds a UIButton to your cell and puts it nicely in its contentView. Decalre a property which references to the button. Like UIButton *myButton. What should be done in the cellForRowAtIndexPath is something like this:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyButtonCell"];           
        if (cell == nil) {
            cell = [[ButtonTableViewCell alloc] initWithReuseIdentifier:@"MyButtonCell"];
        }
        [cell.myButton addTarget:self action:@selector(onDoSomething) forControlEvents:UIControlEventTouchUpInside];
    
        // Do more cell configuration...
        return cell;
    }
    

    I’ve made up the initializer initWithReuseIdentifier which can be easily implemented.

    You assure release of memory with the viewDidUnload method of the UIViewController and the dealloc method.

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

Sidebar

Related Questions

My app runs in portrait mode, but i want to show one screen in
i have uitabbarcontroller with two uiviewcontroller. i want to know if there is possibility
I have a View that I want to display a UITableView in and animate
I have created UISplit view based application. Both, master(root) and detail views are navigation
I have a simple uiviewcontroller with 4 buttons. Every buttonclick event loads a different
I have a UIView called baseView and in that I have initWithFrame where I
I'm currently using UISplitViewController as my app's rootViewController. To present progress dialogs I use
I've a serious problem, and I cannot solve it by myself. I've spent hours
I'm working on a very simple iOS app to get me started programming. It's
I have opted to use a UITableViewController without a nib. I need a UIToolbar

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.