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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:22:49+00:00 2026-05-23T04:22:49+00:00

I have a pretty funny problem which involves loadView , viewDidLoad , viewWillAppear and

  • 0

I have a pretty funny problem which involves loadView, viewDidLoad, viewWillAppearand the state of an ivar. I have a navigation based app. And from my first level view I have a table list and then I click on one of the cells it will take me to second level view (detail view). When I click on a cell I also add an “Office” object (which contains strings like streetAddressand boxAddress) to the view controller that gets pushed. I then populate the detailed view with the contents from the Office object like [box setText:[self.office boxAddress]]; (box is a UILabel). Now what I want to achieve here is that sometimes the stringValue of boxAddress is empty and in those cases I don’t want to add an empty string to the UILabel, instead I want to move the next UILabel up (and take the place of the boxAddress). So therefore I’ve made a conditional check to see if boxAddress is empty if it is it should set up UILabels with specific coordinates and if it’s not empty it should set up UILabels with other specific coordinates.

I understand that you should use viewWillAppear if you want the code to be run everytime the view is loaded. But for some reason it seems that viewWillAppear is only ran when the boxAddress string is noy empty. And if I click on a cell that has an empty boxAddress it will use the value from boxAddressfrom the last cell that I clicked that had a non-empty boxAddress.

I’ll paste my code here to see if you can give me a pointer on what I’m doing wrong here.

// Implement loadView to create a view hierarchy programmatically, 
// without using a nib.
- (void)loadView {

    //allocate the view
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

    //set the view's background color
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidLoad
{

    //add the labels
    name = [[UILabel alloc] initWithFrame:CGRectMake(10.0,10.0,320.0,20.0)];
    [name setBackgroundColor:[UIColor clearColor]];
    [name setTextColor:[UIColor blackColor]];

    street = [[UILabel alloc] initWithFrame:CGRectMake(10.0,30.0,320.0,20.0)];
    [street setBackgroundColor:[UIColor clearColor]];
    [street setTextColor:[UIColor blackColor]];

    //if no box address, move up the rest of the addresses
    if ([[self.office boxAddress] length] == 0) {

        zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,50.0,320.0,20.0)];
        [zip setBackgroundColor:[UIColor clearColor]];
        [zip setTextColor:[UIColor blackColor]];

        phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,320.0,20.0)];
        [phone setBackgroundColor:[UIColor clearColor]];
        [phone setTextColor:[UIColor blackColor]];

        fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,320.0,20.0)];
        [fax setBackgroundColor:[UIColor clearColor]];
        [fax setTextColor:[UIColor blackColor]];

        [self.view addSubview:name];
        [self.view addSubview:street];
        [self.view addSubview:zip];
        [self.view addSubview:phone];
        [self.view addSubview:fax];

    } else {

        box = [[UILabel alloc] initWithFrame:CGRectMake(10.0,50.0,320.0,20.0)];
        [box setBackgroundColor:[UIColor clearColor]];
        [box setTextColor:[UIColor blackColor]];

        zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,320.0,20.0)];
        [zip setBackgroundColor:[UIColor clearColor]];
        [zip setTextColor:[UIColor blackColor]];

        phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,320.0,20.0)];
        [phone setBackgroundColor:[UIColor clearColor]];
        [phone setTextColor:[UIColor blackColor]];

        fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,110.0,320.0,20.0)];
        [fax setBackgroundColor:[UIColor clearColor]];
        [fax setTextColor:[UIColor blackColor]];

        [self.view addSubview:name];
        [self.view addSubview:street];
        [self.view addSubview:box];
        [self.view addSubview:zip];
        [self.view addSubview:phone];
        [self.view addSubview:fax];
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

// viewWillAppear method is run every time the view is loaded as opposite to the viewDidLoad method which only is run once
// in this program DisclosureDetail view needs to be loaded for each detail view with different content each time
- (void)viewWillAppear:(BOOL)animated {

    NSLog(@"%@", [self.office boxAddress]);

    [name setText:[self.office name]];
    [street setText:[self.office streetAddress]];
    if ([[self.office boxAddress] length] > 0) {
        [box setText:[self.office boxAddress]];
    }
    [zip setText:[NSString stringWithFormat:@"%@ %@", [self.office zipCode], [self.office city]]];
    [phone setText:[self.office phoneNo]];
    [fax setText:[self.office faxNo]];

    [super viewWillAppear:animated];
}
  • 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-23T04:22:49+00:00Added an answer on May 23, 2026 at 4:22 am
    if ([[self.office boxAddress] length] > 0) {
        [box setText:[self.office boxAddress]];
    }
    

    If boxAddress‘s length is 0 then box will continue to contain whatever text you set in it the last time the view appeared.

    You’re going to need to hide and show box every time the view appears not just when the view is loaded because the view might be loaded and then used to display multiple different offices.

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

Sidebar

Related Questions

I have two models - category and article. I have pretty much the same
I have a pretty simple user control that I want to bind a ScaleTransform
I have a pretty big query to pull out the information about reports from
I have a pretty simple LIKE search for MySQL that i'd like to transform
I think I have a pretty good grasp on the tenets of a RESTful
OK. So I have a pretty simple question: I want to be able to
I have seen it pretty often so far that for some reason the latitude
I've been creating this pretty large web application for sometime and have been updating
Scenario: There is a title called AJAX, JSON & HTML5! The future of web?
Apologies if this has already been asked and solved but through numerous searches 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.