Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [[self parentViewController] dismissModalViewControllerAnimated:YES];
- @interface BaseView : UIView {}
- @property (nonatomic, assign) UIViewController *parentViewController;
- @end
- @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
- @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
- @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
Add Comment
Please, Sign In to add comment