转载自  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. }

AppDelegate 、UIApplication 的用法的更多相关文章

  1. iOS UIApplication sharedapplication用法

    应用中打开其他应用 我们来讨论一下,在iOS开发中,如何实现从app1打开app2. 基本的思路就是,可以为app2定义一个URL,在app1中通过打开这个URL来打开app2,在此过程中,可以传送一 ...

  2. [转]详解AppDelegate/UIApplication

    一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplica ...

  3. OC中APPDelegate[[UIApplication shareApplication]delegate]]Swift实现

    直接上代码: var myDelegate:AppDelegate? myDelegate = UIApplication.sharedApplication().delegate as? AppDe ...

  4. ios中UIWebview和asiHttprequest的用法

    原文地址为:http://www.cnblogs.com/pengyingh/articles/2343062.htmlasiHttprequest的用法 它对Get请求的响应数据进行缓存(被缓存的数 ...

  5. iOS Method Swizzling和分类的妙用AppDelegate轻量化处理

    http://www.cocoachina.com/ios/20151117/14167.html 简介 在iOS工程中,AppDelegate往往会有上千行,甚至几千行,这样就会给维护AppDele ...

  6. 关于UIApplication单例传值

    由于UIApplication的是一个系统级别的单例,那么就能够省去自己创建单例的方法,将需要需要的类对象,在UIApplication单例内声明一个,通过点语法来实现单个 需要调用的实现单例模式的类 ...

  7. [MISS静IOS开发原创文摘]-AppDelegate存储全局变量和 NSUserDefaults standardUserDefaults 通过模型保存和读取数据,存储自定义的对象

    由于app开发的需求,需要从api接口获得json格式数据并保存临时的 app的主题颜色 和 相关url 方案有很多种: 1, 通过AppDelegate保存为全局变量,再获取 2,使用NSUSerD ...

  8. Core Data的简单用法

    #import "ViewController.h" // 第一步:引入头文件AppDelegate #import "AppDelegate.h" #impo ...

  9. ios开发之AppDelegate

    创建应用程序之后之后,默认有AppDelegate.h文件与AppDelegate.m文件.   AppDelegate为何物?  AppDelegate为整个应用的一个代理,提供程序启动.退出等类似 ...

随机推荐

  1. WebBrowser自动点击链接 广告自动点击 Ads Auto Click

    点击付费广告, 是目前比较流行的一种网络营销模式, 但是,如果你单纯的靠手工一个一个去点, 总觉得不划算  , 能不能实现自动的去点击呢? 答案是肯定的 .NET 里面的 WebBrowser, 可以 ...

  2. 1124. Mosaic(dfs)

    1124 需要想那么一点点吧 一个连通块中肯定不需要伸进手不拿的情况 不是一个肯定会需要这种情况 然后注意一点 sum=0的时候 就输出0就可以了 不要再减一了 #include <iostre ...

  3. 2014年百度之星程序设计大赛 - 资格赛 1004 Labyrinth(Dp)

    题目链接 题目: Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. 函数lock_rec_set_nth_bit

    lock 分配内存 lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t) + n_bytes); 内存分配图 0xxx 2 xxx 0xxx3 ...

  5. SVN备份及其还原 — dump/load方法

    本文中采用最简单的dump/load方法.备份:一个较大的Subsersion版本库想用最少的空间来将它备份下来,用这个命令(请将/repo替换成你的版本库路径)svnadmin dump --del ...

  6. linux制作livecd

    执行: $sudo cp /home/jxg/backup-2011.01.05/backup2011.01.05.squashfs /home/jxg/livecd/casper/filesyste ...

  7. Java知识点:javac命令

    javac命令初窥 注:以下红色标记的参数在下文中有所讲解. 用法: javac <options> <source files> 其中, 可能的选项包括:   -g     ...

  8. 一天一个Java基础——序列化

    1.概念 Java的“对象序列化”能将一个实现了Serializable接口的对象转换成一组byte,这样日后要用这个对象的时候,能把这些byte数据恢复出来,并据此重新构建那个对象. 对象序列化能实 ...

  9. Asp.Net MVC4 系列-- 进阶篇之路由(1)【转】

    http://blog.csdn.net/lan_liang/article/details/22993839?utm_source=tuicool

  10. mysql大内存高性能优化方案

    mysql优化是一个相对来说比较重要的事情了,特别像对mysql读写比较多的网站就显得非常重要了,下面我们来介绍mysql大内存高性能优化方案 8G内存下MySQL的优化 按照下面的设置试试看:key ...