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 a pretty basic windows form app in .Net. All the code is
I have a pretty standard django app, and am wondering how to set the
We have a pretty mature COM dll, which we test using DUnit. One of
I have pretty unassuming preferences screen based on PreferenceActivity . (You can see it
I have pretty simple problem. I have a large file that goes through three
I have pretty much tried everything but still have this same problem. I have
I have a navigation menu generated by Wordpress, meaning I have pretty much no
I have pretty much finished my first working Symbian application, but in my hastened
I have pretty big background of .net, and I've decided that i want to
I would like to have pretty URLs for my tagging system along with all

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.