title: UIView removeFromSuperview causes app to crash
body: <p>Let me explain my problem. I have 3 <code>UIView</code>: a <strong>LoginView</strong>, <strong>LibraryView</strong>, and a <strong>StoreView</strong>. I have this code to switch from one UIView to another:</p>
  
  <pre><code>- (void)showView:(NSInteger)viewTag
  {
    if (viewTag == 1)
    {
        if (self.loginView)
        {
          self.loginView = nil;
          self.loginView.delegate = nil;
        }
  
        LoginView *loginPage = [[LoginView alloc]initWithFrame:self.view.bounds];
        [loginPage setDelegate:self];
  
        self.loginView = loginPage;
  
        [loginPage release];
  
        [self.view addSubview:self.loginView];
  }
  else if(viewTag == 2)
  {
      if (self.libraryView)
      {
          self.libraryView = nil;
          self.libraryView.delegate = nil;
      }
  
      LibraryView *libraryPage = [[LibraryView alloc]initWithFrame:self.view.bounds];
      [libraryPage setDelegate:self];
  
      self.libraryView = libraryPage;
  
      [libraryPage release];
  
      [self.view addSubview:self.libraryView];
  }
  else
  {
      if (self.bookStoreView)
      {
          self.bookStoreView = nil;
          self.bookStoreView.delegate = nil;
      }
  
      BookStoreView *bookStore = [[BookStoreView alloc]initWithFrame:self.view.bounds];
      [bookStore setDelegate:self];
  
      self.bookStoreView = bookStore;
  
      [bookStore release];
  
      [self.view addSubview:self.bookStoreView];
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Basically, that is how I initialize the UIViews. And here are the buttons used to switch between them:</p>
  
  <pre><code>- (void)loginViewToLibraryView
    {
       [self.loginView removeFromSuperview];
       [self showView:2];
    }
  
  - (void)libraryViewToStoreView
    {
       [self.libraryView removeFromSuperview];
       [self showView:3];
    }
  
    //so on...
  </code></pre>
  
  <p>The problem arises when the I call the function <code>libraryViewToLoginView</code> and <code>storeViewToLoginView</code>. The app crashes whenever I call these functions, which is weird because the two functions worked properly before. I checked Profile and it gave me this stack trace:</p>
  
  <pre><code>#   Address     Category Event RefCt  Timestamp    Size Responsible Library   Responsible Caller
  0   0xc4dcac0   CALayer Malloc  1   00:02.233.004   48  UIKit                 -[UIView _createLayerWithFrame:]
  1   0xc4dcac0   CALayer Retain  3   00:02.238.317   0   QuartzCore             CA::Layer::insert_sublayer(CA::Transaction*, CALayer*, unsigned long)
  2   0xc4dcac0   CALayer Release 2   00:02.238.324   0   UIKit                 -[UIView(Internal) _addSubview:positioned:relativeTo:]
  3   0xc4dcac0   CALayer Retain  3   00:02.238.518   0   QuartzCore            -[CALayerArray copyWithZone:]
  4   0xc4dcac0   CALayer Release 2   00:02.238.602   0   UIKit                 -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]
  5   0xc4dcac0   CALayer Retain  3   00:02.238.665   0   QuartzCore            -[CALayerArray copyWithZone:]
  6   0xc4dcac0   CALayer Release 2   00:02.238.796   0   UIKit                 -[UIView(Internal) _didMoveFromWindow:toWindow:]
  7   0xc4dcac0   CALayer Retain  3   00:05.107.397   0   QuartzCore            -[CALayerArray copyWithZone:]
  8   0xc4dcac0   CALayer Release 2   00:05.107.539   0   UIKit                 -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]
  9   0xc4dcac0   CALayer Retain  3   00:05.107.613   0   QuartzCore            -[CALayerArray copyWithZone:]
  10  0xc4dcac0   CALayer Release 2   00:05.107.700   0   UIKit                 -[UIView(Internal) _didMoveFromWindow:toWindow:]
  11  0xc4dcac0   CALayer Retain  2   00:06.105.958   0   QuartzCore            -[CALayerArray copyWithZone:]
  12  0xc4dcac0   CALayer Release 2   00:06.108.134   0   UIKit                 -[UIView dealloc]
  13  0xc4dcac0   CALayer Release 1   00:06.108.492   0   UIKit                 -[UIView dealloc]
  14  0xc4dcac0   CALayer Zombie  -1  00:06.115.332   0   QuartzCore            CA::release_objects(X::List&lt;void const*&gt;*)
  </code></pre>
  
  <p>As you can see, it is a bunch of calls on the CALayer which I don't really understand. I want to understand why is this happening. Can anyone explain?</p>