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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:55:28+00:00 2026-05-18T03:55:28+00:00

I want to reuse the same UIView object as a table header in different

  • 0

I want to reuse the same UIView object as a table header in different UIViewControllers.

I have an ArticleViewController class;

@interface ArticleViewController : UIViewController {
    UIView *headerView;
}

- (UIView *)headerView;

@end

I then implement the headerView get accessor to lazy load the object;

#import "ArticleViewController.h"

@implementation ArticleViewController

- (UIView *)headerView {

    if(headerView)
        return headerView;

    float w = [[self view] bounds].size.width;

    CGRect headerFrame = CGRectMake(0, 0, w, 64);
    CGRect labelFrame = CGRectMake(8, 8, w - 16, 48);

    UILabel *headerText = [[UILabel alloc] initWithFrame:labelFrame];
    [headerText setNumberOfLines:0];
    [headerText setFont:[UIFont boldSystemFontOfSize:14.0]];
    [headerText setBackgroundColor:[UIColor groupTableViewBackgroundColor]];

    headerView = [[UIView alloc] initWithFrame:headerFrame];
    [headerView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [headerView addSubview:headerText];

    return headerView;

}

@end

In another view controller I want to reuse the same headerView object so I’ve declared my interface thus;

@interface CitationViewController : UIViewController {
    UIView *headerView;
}

@property (nonatomic, retain) UIView *headerView;

@end

I use [citationViewController setHeaderView:headerView]; to assign my headerView within ArticleViewController before I push the citationViewController onto my UINavController.

Everything works fine and I get the same header showing when the new view loads. The problem is when I pop the CitationViewController off the UINavController and go back to the old view I’ve lost the headerView object in ArticleViewController.

I’ve tried just passing a pointer to the pointer but I was having trouble getting the **UIView and &headerView to compile. I thought I could just have one object in memory and both views have their own pointers to it. I didn’t get far and went to find another way.

I use the view in a header for a UITableView and I thought it might be this problem; UITableView section header and section footer not updating (redraw problem) but reloading data didn’t fix it.

I then realised that I haven’t incremented the retain count for the headerView so when I pass it over to the CitationViewController and that decrements the retain count I’ll dealloc the object. So then I added a [headerView retain] call before calling the setter but that doesn’t seem to keep the header showing when I reload the old view.

Do I need to have some kind of retain pattern in my get accessor? The custom getter examples always have primative types or simple objects, is the problem because there is another UIView object within headerView as a subview?

I also considered changing the @property (retain) to have assign or copy but got stuck as I don’t implement the copyWithZone protocol and wasn’t sure how to.

I’ve been reading on all these different aspects and understand them in isolation but don’t seem to be able to unite them as a coherent whole. What have I misunderstood in all 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-18T03:55:29+00:00Added an answer on May 18, 2026 at 3:55 am

    Each view object can only ever appear in one view at a time. When you add a view to another view, it will automatically be removed from any other view it’s in.

    You either need to have one instance of headerView per view, or add extra code to ensure the single object is moved around between views. The former would be the common approach (most view objects don’t take up enough memory to worry about) while the latter would be highly unusual.

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

Sidebar

Related Questions

I want to use/reuse C++ object with Objective-C. I have a hello.h that has
I have a bunch of panels with different markup. I want to reuse the
I want to reuse a WebRequest object so that cookies and session would be
I have some shared code I want to reuse in my Xcode projects. The
I have a whole bunch of projects which I want to reuse between the
I want to be able to reuse the same function many times on one
I have a (likely common) case where I would like to reuse the same
I have this 2 methods in the same service class: boolean meth1(DomClass dom1) {
In my iPhone app, I want to be able to reuse the same server-side
I have several methods in the ListAC ListActivity that I want to reuse and

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.