转载自 http://blog.chinaunix.net/uid-26768267-id-3300042.html
- //AppDelegate.h 头文件
- #import <UIKit/UIKit.h>
- @class SwitchViewController;
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- {
- UIApplication *mApp; //新建一个UIApplication对象
- }
- @property (strong, nonatomic) UIWindow *window;
- @property (nonatomic, retain) SwitchViewController *switchViewController; //为窗口转换的类
- @end
- #import "AppDelegate.h"
- #import "SwitchViewController.h"
- @implementation AppDelegate
- @synthesize window = _window;
- @synthesize switchViewController;
- - (void)dealloc
- {
- [_window release];
- [switchViewController release];
- mApp.idleTimerDisabled = NO;
- [super dealloc];
- }
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- //初始化
- switchViewController = [[SwitchViewController alloc]init];
- //这两句话效果一样
- //[[self window]setRootViewController:switchViewController];
- self.window.rootViewController = self.switchViewController;
- [switchViewController release];
- mApp = application;
- application.idleTimerDisabled = YES;
- [[self window]makeKeyAndVisible]; //或[self.window makeKeyAndVisible];
- return YES;
- }
- @end
- //SwitchViewController 类头文件。集中实现窗口跳转
- #import <UIKit/UIKit.h>
- #import "ViewTestone.h"
- #import "ViewController.h"
- @class ViewTestone; //View
- @class ViewController; //View
- typedef int RequestId;
- enum REQUESTVIEW
- {
- REQUEST_TESTONEVIEW,
- };
- @interface SwitchViewController :UIViewController
- {
- }
- @property (nonatomic, retain)ViewTestone *viewTestone;
- @property (nonatomic, retain) UIViewController *previousViewController;
- @property (nonatomic, retain)ViewController *viewController;
- -(void)switchToView:(RequestId)requestId;
- -(void)gotoTestoneView; //跳转到TestoneView
- @end
- //SwitchViewController.m 文件
- #import "SwitchViewController.h"
- @implementation SwitchViewController
- {
- }
- @synthesize viewTestone;
- @synthesize previousViewController;
- @synthesize viewController;
- //当程序加载完AppDelegate里的 didFinishLaunchingWithOptions 方法后就会跳到这里 .下面可把ViewController实现为默认的View加载
- -(void)viewDidLoad
- {
- NSLog(@"----------");
- if(self.viewController ==nil)
- {
- //新建VIEWCONTRollerr 的一个对象
- ViewController *viewcontroll = [[ViewController alloc]initWithNibName:@"ViewConTroller" bundle:nil];
- self.viewController = viewcontroll;
- [viewcontroll release];
- }
- [self.view insertSubview:viewController.view atIndex:0];
- self.previousViewController = self.viewController;
- }
- -(void)viewDidUnload
- {
- self.viewController = nil;
- self.viewTestone = nil;
- [super viewDidLoad];
- }
- -(void)dealloc
- {
- [viewTestone release];
- [viewController release];
- [super dealloc];
- }
- //下面具体实现view之间的跳转。一般的程序都会有很多个这样的窗口跳转。所以把它们集中在一个公共的类里面这样会拿的代码看上去很简洁。明了,
- -(void)switchToView:(RequestId)requestId
- {
- switch (requestId) {
- case REQUEST_TESTONEVIEW:
- [self gotoTestoneView];
- break;
- //....................
- default:
- break;
- }
- }
- //这就到了具体到某一个窗口之间的跳转了。
- -(void)gotoTestoneView
- {
- NSLog(@"test ok~");
- if(self.viewTestone ==nil)
- {
- ViewTestone *testOneView = [[ViewTestone alloc]initWithNibName:@"ViewTestone" bundle:nil];
- self.viewTestone = testOneView;
- [testOneView release];
- }
- if(self.previousViewController.view.superview !=nil)
- {
- [previousViewController.view removeFromSuperview];
- }
- [self.view insertSubview:viewTestone.view atIndex:0];
- self.previousViewController = [self viewTestone];
- }
- @end
- //具体的实现方法。。应用~
- -(void)addButtonToNewView
- {
- //新建一个按键
- UIButton *toviewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- //设置按钮的背景颜色 UIControlStateNormal按钮按之前的颜色 UIControlStateHighlighted反之
- [toviewBtn setImage:[UIImage imageNamed:@"v_exit.png"]forState:UIControlStateNormal ] ;
- //CGRect r;
- //r = CGRectMake(120, 300, 100, 30);
- //设置按钮的大小
- toviewBtn.frame = CGRectMake(120, 300, 100, 30);;
- //为按钮添加事件
- [toviewBtn addTarget:self action:@selector(toOntViewPress:) forControlEvents:UIControlEventTouchUpInside];
- //加载到当前的view
- [self.view addSubview:toviewBtn];
- }
- -(void)toOntViewPress:(id)sender
- {
- NSLog(@"successfull");
- //前面长长的AppDelegate SwitchViewController 都是为了下面这两句话。两句话就可以轻松的跳转到你想要去的页面。这样就显得很方便。
- AppDelegate *app = [[UIApplication sharedApplication]delegate];
- [app.switchViewController switchToView:0];
- }