开发者

iPhone View Switching basics

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.supervie开发者_StackOverflow社区w != 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];
 }
}


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];
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜