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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:49:35+00:00 2026-05-26T02:49:35+00:00

I ran into something odd today that maybe someone knows something about. I have

  • 0

I ran into something odd today that maybe someone knows something about. I have a subclass of UIViewController and its associated NIB. I set the labels in the UIViewController methods and all that works fine.

Now from another class, I create that ViewController again because I want to reuse it. I do this:

MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
vc.titleLabel.text = @"testing";
vc.myTextLabel.text = @"yo";
self.navigationController pushViewController:vc animated:NO];
[vc release];

This does NOT work. I have no idea why this does not work. I would think I would set all the labels, then show the view controller by pushing it onto the stack.

However, if I do this:

[vc.view setNeedsDisplay]; // why here???
MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
vc.titleLabel.text = @"testing";
vc.myTextLabel.text = @"yo";
self.navigationController pushViewController:vc animated:NO];
[vc release];

This DOES work. This does not make sense to me. I thought setNeeds Display was called AFTER a view needs to be redrawn. If I move setNeedsDisplay to the end of the block it does NOT work. It only works at the beginning of the block which is very odd to me. Any one encounter this before or know why it works this way? Thanks.

  • 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-26T02:49:35+00:00Added an answer on May 26, 2026 at 2:49 am

    The reason is that a view controller’s view is lazily-loaded. This means the controller’s view is only loaded from a nib (or via -loadView) when you access the view property for the first time. If you attempt to access the labels before the view has been loaded, they will be nil and any messages you send to them will be no-ops.

    So to force the view to load, you can do this:

    /* make sure the view is loaded */
    [vc view];
    /* Access the label properties */
    vc.titleLabel.text = @"testing";
    

    However, forcing the view to load may not be a good idea in all situations, especially if the view controller is not going to be displayed immediately and you want to save memory.

    In this case you can create the labels in the controller’s init method so they always exist, and add them to the view controller’s view manually in -viewDidLoad, rather than in your nib. This will allow the standard lazy-loading behaviour to work, but users of your class can still set properties on the labels before the view is loaded.

    An alternative is to expose simple NSString properties with associated ivars on the view controller to represent any titles or text in the view. Then in your -viewDidLoad you can set the text of the labels to the value of these properties. Users of your view controller can then set these properties before the view has loaded.

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

Sidebar

Related Questions

I just started playing around with threading today and I ran into something that
So I ran into something interesting that I didn't realize about the ternary operator
I ran into something that seems odd. SQL Server appears to be rounding some
I ran into a problem... perhaps someone bumped into something similar: I have an
I ran into something odd, and I'm not precisely sure why it is behaving
I ran into something a little odd this morning and thought I'd submit it
I just ran into something in Visual Studio 2010 RC that wasn't previously happening
I was doing some performance metrics and I ran into something that seems quite
I ran into an issue with something that I am probably just overlooking. I
I ran into something that I never thought of before and it stumped me.

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.