AppDelegate 、UIApplication 简单的用法
2015-08-15 11:07
483 查看
//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];
}
#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];
}
相关文章推荐
- 安卓开发之简单的短信操作模块
- IOS第十天(1:QQ好友列表 ,自定义的headview,代理 ,通知 ,black的使用)
- android开发之bitmap使用
- android开发之bitmap使用
- android开发之bitmap使用
- android开发之bitmap使用
- IOS第九天(3:QQ聊天界面通知的使用)
- WordPress固定链接修改报错:Object not found的解决方法
- Objective-C:三种文件导入的方式比较
- IOS第九天(2:QQ聊天界面键盘优化 和自动回复)
- android中设置控件背景的3种方式
- inflate与setcontentview及findviewbyid(二)
- Android跨进程访问(AIDL服务)
- Android中的文字识别(论文精简版)
- 分享:高效开发Android App的10个建议
- android中src和background区别
- [盈利指导] App推广干货,排名数据分析优化效果
- UIApplication
- webrtc 编译完成,安装时错误
- 【干货】最新app源码下载:200款优秀Android项目源码