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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:28:49+00:00 2026-05-12T16:28:49+00:00

Can someone please school me on the proper way to load a view hierarchy

  • 0

Can someone please school me on the proper way to load a view hierarchy from a nib file. I am using the loaded view as a template to stamp out a family of views and the current approach I am using is subtly broken. I don’t appear to be copy-ing or retain-ing when I should be. Here’s the relevant code:

// pageSet is a list of view tag numbers I'll be using
for (NSNumber *n in pageSet) {

    NSUInteger viewTag = [n integerValue];

    // Ingest the nib. Should there be a copy or retain here?
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RandomStripe" owner:self options:nil];

    // Pull the view from the nib. Should there be a copy or retain here?
    MyView *view = (MyView *)[topLevelObjects objectAtIndex:0];

    // The view has a label as it's subview
    UILabel *pageNumberLabel = [view.subviews objectAtIndex:0];
    pageNumberLabel.text     = [NSString stringWithFormat:@"%d", viewTag];

    CGFloat xOffset = ((float) viewTag) * self.scrollView.bounds.size.width;

    view.frame = CGRectMake(xOffset, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
    view.tag = viewTag;

    // Insert the view as a child of my containerView
    [self.containerView addSubview:view];

} // for (pageSet)

This has be making my head hurt for while now?

Cheers,
Doug

  • 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-12T16:28:49+00:00Added an answer on May 12, 2026 at 4:28 pm

    If you use IBOutlets from within Interface Builder back to your code, things would be a little easier. That way as soon as you try to access the UIView outlet you have set up, it gets loaded, with all its children, and then the UIView’s initWithCoder will get called (useful if you have subclassed it).

    @property (retain, nonatomic) IBOutlet iiView   *iiView;
    

    Otherwise, I do this:

    if (self.numberView == nil) {
        NumberView  *numView = [[NumberView alloc] initWithNibName:@"NumberView" bundle:[NSBundle mainBundle]];
        self.numberView = numView;
        [numView release];
    }
    

    (With an IBOutlet for numberView, I just go ahead and start using numberView instead of the code above)

    In both conditions, all the subviews – the children – of numberView will get loaded at the same time. If I needed to access a label or button I would do an IBOutlet for those too so I won’t have to traverse the view hierarchy looking for them.

    IBOutlet UIButton *nextButton,
              *stopButton1,
              *stopButton2,
              *infoButton,
              *bitsonthegoButton;
    IBOutlet UILabel *pointsLabel1,
             *pointsLabel2,
             *totalPointsLabel1,
             *totalPointsLabel2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please provide an example of using set_bound() method from Test::MockObject module? Can
Can someone please explain to me how can I sign a CAB file? I'm
Can someone please tell me what I'm doing wrong? I'm using the following arguments
can someone please point me at documentation that shows the output from the systeminfo
can someone please identify what type of view this is, as i want to
Can someone please point me to the easiest way to have a timer in
Can someone please derive a concrete example from the following: http://www.urdalen.com/blog/?p=210 ..that shows how
can someone please explain what's the exports variable for: copied from backbone.js, I also
Can someone please help me with this? I want to modify the xml file
Can someone please tell me how does client side regular expressions validation differ from

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.