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