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

  • Home
  • SEARCH
  • 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 3791692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:30:30+00:00 2026-05-19T12:30:30+00:00

I have a UIViewController that materializes its view in loadView (i.e. no nib). Per

  • 0

I have a UIViewController that materializes its view in loadView (i.e. no nib). Per the documentation (and via confirmation in code), loadView and consequently, viewDidLoad will not get called until UIViewController’s view is accessed the first time.

I have another class that instantiates the UIViewController and calls a number of methods on it before the view itself is referenced. Many of these methods modify the view / subviews in the UIViewController. Unfortunately, the view / subviews are not instantiated at this point. (I can easily just add a reference to the view before the other method calls, but this requires effort and understanding of an invisible contract on the user of my UIViewController).

How do I handle methods on the UIViewController which modify the view / subviews? In each of my UIViewController methods, I can check if the view is loaded, but you aren’t supposed to call loadView directly. I could put a “self.view;” in the method to presumably assure the view is loaded, but this seems pretty hackish (and of course causes a clang warning).

Additionally, this isn’t just a one time thing, since obviously the view could be unloaded during memory events, allowing this state to happen quite often.

I feel like I’m missing something fairly simple, but I haven’t been able to find it. What is the appropriate way to handle this state / initialization issue?

  • 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-19T12:30:31+00:00Added an answer on May 19, 2026 at 12:30 pm

    Basically the answer to this kind of thing is to not expose components of your views through your view controller as other objects probably shouldn’t be accessing them. So, if your view controller manages a view that renders a person’s name, for example, instead of doing something like this:

    PersonNameController* pnc = [[PersonNameController alloc] initWithNibName:nil bundle:nil];
    [[pnc nameLabel] setText:@"Jason"];
    [[self navigationController] pushViewController:pnc animated:YES];
    

    You would separate the data/model bits from the view bits and expose a ‘name’ property on your view controller. Then you can have this simple example code which always works properly (it requires a bit more code, but it saves you from headaches like this and, to be perfectly honest, is what MVC is about):

    PersonNameController* pnc = [[PersonNameController alloc] initWithNibName:nil bundle:nil];
    [pnc setName:@"Jason"];
    [[self navigationController] pushViewController:pnc animated:YES];
    

    And the implementation of such a controller:

    @interface PersonNameController : UIViewController {
     @private
      NSString* name_;
    }
    
    // other people use this to pass model data to the controller
    @property(nonatomic,copy) NSString* name;   
    
    @end
    
    @interface PersonNameController()
    
    // for us only
    @property(nonatomic,assign) UILabel* nameLabel;
    
    @end
    
    @implementation PersonNameController
    
    // properties
    @synthesize nameLabel;
    @synthesize name = name_;
    
    - (void)setName:(NSString*)value
    {
      if( [value isEqualToString:name_] ) return;
      [name_ autorelease];
      name_ = [value copy];
      [[self nameLabel] setText:name_?:@""];
    }
    
    // view lifecycle
    - (void)loadView
    {
      // ob fake frame for now
      UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
      UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake( 10, 10, 300, 37)];
      [view addSubview:label];
      [self setView:view];
      [self setNameLabel:label];
    
      // Set the name on the label if one's set already
      [label setText:name_?:@""];
    
      // clean up
      [view release];
      [label release];
    }
    
    // take care of unloads
    - (void)viewDidUnload
    {
      // no need to release, but set nil so it's not used while the view
      // is unloaded
      nameLabel = nil;
      [super viewDidUnload];
    }
    
    - (void)dealloc
    {
      // nameLabel is assign (owned by our view) so we don't release here
      // but set to nil just to be good citizens
      nameLabel = nil;
      [name_ release];
      [super dealloc];
    }
    
    @end
    

    Quick Note: This was just typed up in this little box and I’m really tired, so it’s not checked for total syntax correctness, etc. It’s meant as a quick example not something to cut and paste.

    Also, if you had a number of properties that needed to be updated each time, you might create a single utility method like -udpateLabels or something that updates them all. Then you call that from each setter and from viewDidLoad or loadView. That way you’ll have everything in one place (at the expense of some performance). If, after profiling, you spend too much time in that method, you can break it out as appropriate.

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

Sidebar

Related Questions

I have a UIViewController that is being pushed via a UINavigationViewController. The viewController's view
I have a UIViewController that I want to load from a NIB that has
I have a UIViewController that manages the display of some data. When the user
First a little background info: I have UIViewController that contains a UITableView. In the
I have a UIViewController that is pushed onto a UINavigationController and is currently displayed.
I have a UIViewController that returns YES in shouldAutorotateToInterfaceOrientation: for UIDeviceOrientationPortrait and NO for
I have a UIViewController that implements UITextViewDelegate and is connected as the delegate to
I have a UIViewController that instantiates several UIImageViews on the screen. Is it possible
I have a UIViewController that contains a subview and a UIToolbar. I'm trying to
I have a UIViewController that I want to display a second UIViewController as a

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.