Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, you can use a single instance of a view in a number of views/viewControllers. Typically I do the same with Views that carry advertisements. </p> <p>You pass them along as you would do with any other object. If you do not create it in Interface Bulder (I suggest creating it programmatically) then you may want to define it within your application delegate rather than a view controller and pass it to the individual view controllers that make use of it. </p> <p>Within the view controller just add it as sub view accordingly, as you would do it with any other view too. </p> <p>There is one thing though. When you add this view to another super view for the second time or more then it will be removed from its prior super view. That means that you will have to add it as super view again, when its prior super view becomes visible again. A view can only be part of one view hierarchy at a time. </p> <hr> <p>Sample code, thanks to Gordon: </p> <pre><code>/* Untested and simplified */ AppDelegate.h: @property ( strong, nonatomic) ReuseableView reuseableView </code></pre> <p>; </p> <p>AppDelegate.m </p> <pre><code>@synthesize reuseableView; /* in didFinishLaunchingWithOptions ...*/ reuseableView = [[alloc] init]; // or init from nib, initwithframe, etc. </code></pre> <p>viewController.m</p> <pre><code>/* In each view controller that uses the view */ - (void) viewWillAppear:(BOOL)animated { [self.view addSubview:((AppDelegate*)[UIApplication sharedApplication].delegate).reuseableView]; } - (void) viewWillDisappear:(BOOL)animated { [((AppDelegate*)[UIApplication sharedApplication].delegate).ReuseableView removeFromSuperview]; </code></pre> <p>} </p> <hr> <p>I am not quite sure whether this <code>removeFromSuperview</code> is really required. The next <code>addSubview</code> will remove it from its existing superview anyway and if <code>addSubview</code> is called on the same super view twice in a row then it does not do any harm. However, it is save using <code>removeFromSuperview</code> at this point. </p> <p>Well, summarized that is basically it. You define, create and store your shared view (the reusableView in Gordon's example) at a common place. The application delegate is a good place for that. In each view's controller that uses the shared view you fetch it from the delegate and add it as subview to the current view.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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