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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:31:20+00:00 2026-06-03T18:31:20+00:00

[I have this working, but I don’t understand why my fix to make it

  • 0

[I have this working, but I don’t understand why my “fix” to make it work did so.]

As part of a learning exercise, I’m creating a simple table. When the user selects a cell in the table, I want it to go a second UIViewController. THe second UIViewController has a label that shows the text from the cell selected.

The “parent” class has this method to create the child & set the text:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  child = [[WDRViewControllerFirstChild alloc] initWithNibName:nil bundle:nil];
  child.title = [colors objectAtIndex:indexPath.row];
  child.labelText = [colors objectAtIndex:indexPath.row];
  [self.navigationController pushViewController:child animated:YES];  
}

Then in WDRViewControllerFirstChild, I have two methods. If I approach it this way, everything works.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
      colorMap = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil] forKeys:[NSArray arrayWithObjects:@"red", @"green", @"blue", nil]];
//      Adding the subview here won't work.  Why?
//      [self.view addSubview:label];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  [self.view addSubview:label];
  label.text = labelText;
  label.textAlignment = UITextAlignmentCenter;
  label.backgroundColor = [UIColor whiteColor];
  self.view.backgroundColor = [colorMap objectForKey:labelText];
}

Originally, I assigned the child to the subview in the init call, however that didn’t work. That is, the label of the text wouldn’t properly show the text of the item that had been selected. (It would be blank.)

I added some NSLog calls and additionally found that if I move the addSubview call from viewDidLoad to the init, the value of labelText is null. However, in the form above, it’s properly set.

I’m happy it’s working, but I don’t understand why one work & the other does. In particular, I’m really confused why setting labelText works based upon where I call addSubview.

Any insights?

  • 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-06-03T18:31:23+00:00Added an answer on June 3, 2026 at 6:31 pm

    -addSubview will only work if the view is actually completely loaded out of the XIB, else it sends a call to nil and produces nil. By the time -initWithNibName:bundle: is called, the OS is most likely defrosting (literally) the XIB you specified and setting it up, so the view property is nil. at -viewDidLoad you can be reasonably assured as to the existence of the view, so that’s where most setup work is done. as for the (NULL) label text, whatever the iVar labelText is, you didn’t instantiate it. Remove that line.

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

Sidebar

Related Questions

I have been working on this for days but I don't get it so
I'm not sure why this isn't working. I don't have any errors, but what
I have this working in C#, but don't know where to go with Objective
I have this code working, however I don't quite understand how it is managing
I currently have this working but it requires me to have a static method
I think I have this working but I need advice. I'd like to know
I have this regex working but now need to allow numbers without the decimal
I have this query working but it is not returning back exactly what I
I have this working great, but I'd like a deeper understanding of what is
I have this working code, but now i need to be able to change

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.