//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]; }