AppDelegate 、UIApplication 的用法

转载自  http://blog.chinaunix.net/uid-26768267-id-3300042.html

  1. //AppDelegate.h 头文件
  2. #import <UIKit/UIKit.h>
  3. @class SwitchViewController;
  4. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  5. {
  6. UIApplication *mApp; //新建一个UIApplication对象
  7. }
  8. @property (strong, nonatomic) UIWindow *window;
  9. @property (nonatomic, retain) SwitchViewController *switchViewController; //为窗口转换的类
  10. @end
  11. #import "AppDelegate.h"
  12. #import "SwitchViewController.h"
  13. @implementation AppDelegate
  14. @synthesize window = _window;
  15. @synthesize switchViewController;
  16. - (void)dealloc
  17. {
  18. [_window release];
  19. [switchViewController release];
  20. mApp.idleTimerDisabled = NO;
  21. [super dealloc];
  22. }
  23. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  24. {
  25. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  26. //初始化
  27. switchViewController = [[SwitchViewController alloc]init];
  28. //这两句话效果一样
  29. //[[self window]setRootViewController:switchViewController];
  30. self.window.rootViewController = self.switchViewController;
  31. [switchViewController release];
  32. mApp = application;
  33. application.idleTimerDisabled = YES;
  34. [[self window]makeKeyAndVisible]; //或[self.window makeKeyAndVisible];
  35. return YES;
  36. }
  37. @end
  38. //SwitchViewController 类头文件。集中实现窗口跳转
  39. #import <UIKit/UIKit.h>
  40. #import "ViewTestone.h"
  41. #import "ViewController.h"
  42. @class ViewTestone; //View
  43. @class ViewController; //View
  44. typedef int RequestId;
  45. enum REQUESTVIEW
  46. {
  47. REQUEST_TESTONEVIEW,
  48. };
  49. @interface SwitchViewController :UIViewController
  50. {
  51. }
  52. @property (nonatomic, retain)ViewTestone *viewTestone;
  53. @property (nonatomic, retain) UIViewController *previousViewController;
  54. @property (nonatomic, retain)ViewController *viewController;
  55. -(void)switchToView:(RequestId)requestId;
  56. -(void)gotoTestoneView; //跳转到TestoneView
  57. @end
  58. //SwitchViewController.m 文件
  59. #import "SwitchViewController.h"
  60. @implementation SwitchViewController
  61. {
  62. }
  63. @synthesize viewTestone;
  64. @synthesize previousViewController;
  65. @synthesize viewController;
  66. //当程序加载完AppDelegate里的 didFinishLaunchingWithOptions 方法后就会跳到这里 .下面可把ViewController实现为默认的View加载
  67. -(void)viewDidLoad
  68. {
  69. NSLog(@"----------");
  70. if(self.viewController ==nil)
  71. {
  72. //新建VIEWCONTRollerr 的一个对象
  73. ViewController *viewcontroll = [[ViewController alloc]initWithNibName:@"ViewConTroller" bundle:nil];
  74. self.viewController = viewcontroll;
  75. [viewcontroll release];
  76. }
  77. [self.view insertSubview:viewController.view atIndex:0];
  78. self.previousViewController = self.viewController;
  79. }
  80. -(void)viewDidUnload
  81. {
  82. self.viewController = nil;
  83. self.viewTestone = nil;
  84. [super viewDidLoad];
  85. }
  86. -(void)dealloc
  87. {
  88. [viewTestone release];
  89. [viewController release];
  90. [super dealloc];
  91. }
  92. //下面具体实现view之间的跳转。一般的程序都会有很多个这样的窗口跳转。所以把它们集中在一个公共的类里面这样会拿的代码看上去很简洁。明了,
  93. -(void)switchToView:(RequestId)requestId
  94. {
  95. switch (requestId) {
  96. case REQUEST_TESTONEVIEW:
  97. [self gotoTestoneView];
  98. break;
  99. //....................
  100. default:
  101. break;
  102. }
  103. }
  104. //这就到了具体到某一个窗口之间的跳转了。
  105. -(void)gotoTestoneView
  106. {
  107. NSLog(@"test ok~");
  108. if(self.viewTestone ==nil)
  109. {
  110. ViewTestone *testOneView = [[ViewTestone alloc]initWithNibName:@"ViewTestone" bundle:nil];
  111. self.viewTestone = testOneView;
  112. [testOneView release];
  113. }
  114. if(self.previousViewController.view.superview !=nil)
  115. {
  116. [previousViewController.view removeFromSuperview];
  117. }
  118. [self.view insertSubview:viewTestone.view atIndex:0];
  119. self.previousViewController = [self viewTestone];
  120. }
  121. @end
  122. //具体的实现方法。。应用~
  123. -(void)addButtonToNewView
  124. {
  125. //新建一个按键
  126. UIButton *toviewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  127. //设置按钮的背景颜色 UIControlStateNormal按钮按之前的颜色 UIControlStateHighlighted反之
  128. [toviewBtn setImage:[UIImage imageNamed:@"v_exit.png"]forState:UIControlStateNormal ] ;
  129. //CGRect r;
  130. //r = CGRectMake(120, 300, 100, 30);
  131. //设置按钮的大小
  132. toviewBtn.frame = CGRectMake(120, 300, 100, 30);;
  133. //为按钮添加事件
  134. [toviewBtn addTarget:self action:@selector(toOntViewPress:) forControlEvents:UIControlEventTouchUpInside];
  135. //加载到当前的view
  136. [self.view addSubview:toviewBtn];
  137. }
  138. -(void)toOntViewPress:(id)sender
  139. {
  140. NSLog(@"successfull");
  141. //前面长长的AppDelegate SwitchViewController 都是为了下面这两句话。两句话就可以轻松的跳转到你想要去的页面。这样就显得很方便。
  142. AppDelegate *app = [[UIApplication sharedApplication]delegate];
  143. [app.switchViewController switchToView:0];
  144. }
上一篇:Java设计模式:Simple Factory(简单工厂)模式


下一篇:Java [Leetcode 119]Pascal's Triangle II