views:

214

answers:

2

I am just trying to get my head around simple view switching for the iPhone and have created a simple app to try and help me understand it.

I have included the code from my root controller used to switch the views. My app has a single toolbar with three buttons on it each linking to one view. Here is my code to do this but I think there most be a more efficient way to achieve this? Is there a way to find out / remove the current displayed view instead of having to do the if statements to see if either has a superclass?

I know I could use a tab bar to create a similar effect but I am just using this method to help me practice a few of the techniques.

-(IBAction)switchToDataInput:(id)sender{
 if (self.dataInputVC.view.superview == nil) {
  if (dataInputVC == nil) {
   dataInputVC = [[DataInputViewController alloc] initWithNibName:@"DataInput" bundle:nil];
  }
  if (self.UIElementsVC.view.superview != nil) {
   [UIElementsVC.view removeFromSuperview];
  } else if (self.totalsVC.view.superview != nil) {
   [totalsVC.view removeFromSuperview];
  }

  [self.view insertSubview:dataInputVC.view atIndex:0];
 }
}

-(IBAction)switchToUIElements:(id)sender{
 if (self.UIElementsVC.view.superview == nil) {
  if (UIElementsVC == nil) {
   UIElementsVC = [[UIElementsViewController alloc] initWithNibName:@"UIElements" bundle:nil];
  }
  if (self.dataInputVC.view.superview != nil) {
   [dataInputVC.view removeFromSuperview];
  } else if (self.totalsVC.view.superview != nil) {
   [totalsVC.view removeFromSuperview];
  }

  [self.view insertSubview:UIElementsVC.view atIndex:0];
 }

}

-(IBAction)switchToTotals:(id)sender{
 if (self.totalsVC.view.superview == nil) {
  if (totalsVC == nil) {
   totalsVC = [[TotalsViewController alloc] initWithNibName:@"Totals" bundle:nil];
  }
  if (self.dataInputVC.view.superview != nil) {
   [dataInputVC.view removeFromSuperview];
  } else if (self.UIElementsVC.view.superview != nil) {
   [UIElementsVC.view removeFromSuperview];
  }

  [self.view insertSubview:totalsVC.view atIndex:0];
 }
}
+1  A: 

I would recommend not re-creating each view every time you want to display, but rather just bringing the correct subview to the front when required. Something like:

-(void)init{
  // The 3 view controllers below are ivars, so we can access in other methods 
  dataInputVC = [[DataInputViewController alloc] initWithNibName:@"DataInput" bundle:nil];   
  UIElementsVC = [[UIElementsViewController alloc] initWithNibName:@"UIElements" bundle:nil];
  totalsVC = [[TotalsViewController alloc] initWithNibName:@"Totals" bundle:nil];

  // Add as subviews (rearrange so that correct view appears first)
  [self.view addSubview:dataInputVC.view];
  [self.view addSubview:UIElementsVC.view];
  [self.view addSubview:totalsVC.view];
}

-(IBAction)switchToDataInput:(id)sender{
  [self.view bringSubviewToFront:dataInputVC.view];
}

-(IBAction)switchToUIElements:(id)sender{
  [self.view bringSubviewToFront:UIElementsVC.view];
}

-(IBAction)switchToTotals:(id)sender{
  [self.view bringSubviewToFront:totalsVC.view];
}
pheelicks
Thanks for the reply.I think the if statement should prevent the view from being created every time but only when it does not already exist. As I have included in the didReceiveMemoryWarning a section of code which will remove the inactive views so if this were to happen then a new view would need to be created and also its a form of lazy loading if the user never clicks on one of the views then it will never be loaded. Does that work?I like what you have done there it is much neater but would it not incur a memory penalty having all three views loaded at the same time?Thanks
Daniel Granger
If you want your code to work with the didReceiveMemoryWarning then include your lazy loading code in each of the the IB actions, instead of in an init method like I have done. The thing that is ugly about your code are the removeFromSuperview calls
pheelicks
+1  A: 

Don't reinvent UITabBarController. Screw the toolbar and replace it with a tab bar, and then all of this behavior will be built-in out of the box for you. That should be a lot easier! Let me know how it turns out.

Jonathan Sterling
Hi Jonathan,Thanks for the reply, I understand that I could use UITabBarController to do exactly this! The reason I am not though is I am using this as a learning process to help me get my head around using views and using a toolbar was just a quick and dirty way to create three buttons to switch to three different views but doing all the work myself so that I can learn from it.Thanks
Daniel Granger