You can use the following approach:
BaseView.h - all other views are inherited from this view:
@interface BaseView : UIView {} @property (nonatomic, assign) UIViewController *parentViewController; @end
BaseView.m
@implementation BaseView @synthesize parentViewController; - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { // Initialization code } return self; } - (void)dealloc { [self setParentViewController:nil]; [super dealloc]; } @end
Other views may inherit from BaseView. Their code should be something like this:
@implementation FirstView - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self setBackgroundColor:[UIColor yellowColor]]; UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)]; [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button1 setBackgroundColor:[UIColor whiteColor]]; [button1 setCenter:CGPointMake(160.0f, 360.0f)]; [button1 setTitle:@"Show Second View" forState:UIControlStateNormal]; [button1 addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button1]; [button1 release]; UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)]; [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button2 setBackgroundColor:[UIColor whiteColor]]; [button2 setCenter:CGPointMake(160.0f, 120.0f)]; [button2 setTitle:@"Show Sub View" forState:UIControlStateNormal]; [button2 addTarget:self action:@selector(showView:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button2]; [button2 release]; } return self; } - (void)showView:(id)sender { if (viewController == nil) { viewController = [[SubViewController alloc] initWithNibName:nil bundle:nil]; } [self addSubview:viewController.view]; } - (void)switchView:(id)sender { SecondView *secondView = [[SecondView alloc] initWithFrame:self.frame]; [self.parentViewController performSelector:@selector(switchView:) withObject:secondView]; [secondView release]; } - (void)dealloc { [viewController release]; [super dealloc]; } @end
Finally, you will need 1 UIViewController for all views. The switchView: method is called to change the current view. The code for the UIViewController could be something like this:
@implementation ViewController - (void)loadView { FirstView *firstView = [[FirstView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)]; [firstView setParentViewController:self]; [self setView:firstView]; [firstView release]; } - (void)switchView:(BaseView *)newView { [newView setParentViewController:self]; [self retain]; [self setView:newView]; [self release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewDidUnload { [super viewDidUnload]; } - (void)dealloc { [super dealloc]; } @end
You can download the sample application (within the next hour): http://dl.dropbox.com/u/6487838/ViewSwitch.zip
Wolfgang schreurs
source share