关于 AppDelegate 、UIApplication 简单的用法

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/qingjoin/archive/2012/08/03/2621785.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值