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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。