code_junkie

Programatically Switching Views in Cocoa Touch

Nov 13th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. [[self parentViewController] dismissModalViewControllerAnimated:YES];
  2.  
  3. @interface BaseView : UIView {}
  4. @property (nonatomic, assign) UIViewController *parentViewController;
  5. @end
  6.  
  7. @implementation BaseView
  8. @synthesize parentViewController;
  9.  
  10. - (id)initWithFrame:(CGRect)frame {
  11. if ((self = [super initWithFrame:frame])) {
  12. // Initialization code
  13. }
  14. return self;
  15. }
  16.  
  17. - (void)dealloc {
  18. [self setParentViewController:nil];
  19. [super dealloc];
  20. }
  21.  
  22. @end
  23.  
  24. @implementation FirstView
  25.  
  26. - (id)initWithFrame:(CGRect)frame {
  27. if ((self = [super initWithFrame:frame])) {
  28. [self setBackgroundColor:[UIColor yellowColor]];
  29.  
  30. UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)];
  31. [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  32. [button1 setBackgroundColor:[UIColor whiteColor]];
  33. [button1 setCenter:CGPointMake(160.0f, 360.0f)];
  34. [button1 setTitle:@"Show Second View" forState:UIControlStateNormal];
  35. [button1 addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
  36. [self addSubview:button1];
  37. [button1 release];
  38.  
  39. UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)];
  40. [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  41. [button2 setBackgroundColor:[UIColor whiteColor]];
  42. [button2 setCenter:CGPointMake(160.0f, 120.0f)];
  43. [button2 setTitle:@"Show Sub View" forState:UIControlStateNormal];
  44. [button2 addTarget:self action:@selector(showView:) forControlEvents:UIControlEventTouchUpInside];
  45. [self addSubview:button2];
  46. [button2 release];
  47. }
  48.  
  49. return self;
  50. }
  51.  
  52. - (void)showView:(id)sender {
  53. if (viewController == nil) {
  54. viewController = [[SubViewController alloc] initWithNibName:nil bundle:nil];
  55. }
  56.  
  57. [self addSubview:viewController.view];
  58. }
  59.  
  60. - (void)switchView:(id)sender {
  61. SecondView *secondView = [[SecondView alloc] initWithFrame:self.frame];
  62. [self.parentViewController performSelector:@selector(switchView:) withObject:secondView];
  63. [secondView release];
  64. }
  65.  
  66. - (void)dealloc {
  67. [viewController release];
  68. [super dealloc];
  69. }
  70.  
  71. @end
  72.  
  73. @implementation ViewController
  74.  
  75. - (void)loadView {
  76. FirstView *firstView = [[FirstView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
  77. [firstView setParentViewController:self];
  78. [self setView:firstView];
  79. [firstView release];
  80. }
  81.  
  82. - (void)switchView:(BaseView *)newView {
  83. [newView setParentViewController:self];
  84. [self retain];
  85. [self setView:newView];
  86. [self release];
  87. }
  88.  
  89. - (void)didReceiveMemoryWarning {
  90. [super didReceiveMemoryWarning];
  91. }
  92.  
  93. - (void)viewDidUnload {
  94. [super viewDidUnload];
  95. }
  96.  
  97. - (void)dealloc {
  98. [super dealloc];
  99. }
  100.  
  101. @end
Add Comment
Please, Sign In to add comment