Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading UIView from a nib file without guesswork
    primarykey
    data
    text
    <p>Ok, here's another question.</p> <p>I am creating a UIView called <code>ProgressView</code> that is a semi-transparent view with an activity indicator and a progress bar. </p> <p>I want to be able to use this view throughout different view controllers in my app, when required.</p> <p>I know of 3 different ways of doing this (but I am only interested in one):</p> <p>1) Create the entire view programatically, instantiate and configure as required. No worries I get that one.</p> <p>2) Create the UIView in interface builder, add the required objects and load it using a method like the below. Problem with this is that we are basically guessing that the view is the objectAtIndex:0 because nowhere in the documentation I found a reference to the order of the elements returned from the <code>[[NSBundle mainBundle] loadNibName:</code> function.</p> <pre><code>NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"yournib" owner:self options:nil]; UIView *myView = [nibContents objectAtIndex:0]; myView.frame = CGRectMake(0,0,300,400); //or whatever coordinates you need [scrollview addSubview:myView]; </code></pre> <p>3) Subclass UIViewController and let it manage the view as per normal. In this case I would never be actually pushing the view controller onto the stack, but only its main view:</p> <pre><code>ProgressViewController *vc = [[ProgressViewController alloc] initWithNibName:@"ProgressView" bundle:nil]; [vc.view setCenter:CGPointMake(self.view.center.x, self.view.center.y)]; [self.view addSubview:vc.view]; [vc release]; </code></pre> <p>As far as I can tell, #3 is the the correct way of doing this (apart from programatically) but I am not entirely sure if it is safe to release the ProgressView's view controller whilst another controller's view is retaining its main view (gut feel says it is going to leak?)?</p> <p>What do I do in terms of memory management in this case, where and when should I release the ProgressView's view controller?</p> <p>Thanks in advance for your thoughts.</p> <p>Cheers,</p> <p>Rog</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload