zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS开发那些事-响应内存警告

ios内存响应开发 那些 警告
2023-09-14 08:59:50 时间
好的应用应该在系统内存警告情况下释放一些可以重新创建的资源。在iOS中我们可以在应用程序委托对象、视图控制器以及其它类中获得系统内存警告消息。 p 1、应用程序委托对象 /p p 在应用程序委托对象中接收内存警告消息,需要重写applicationDidReceiveMemoryWarning:方法。AppDelegate的代码片段: /p pre name= code
好的应用应该在系统内存警告情况下释放一些可以重新创建的资源。在iOS中我们可以在应用程序委托对象、视图控制器以及其它类中获得系统内存警告消息。

1、应用程序委托对象

在应用程序委托对象中接收内存警告消息,需要重写applicationDidReceiveMemoryWarning:方法。AppDelegate的代码片段:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application

NSLog(@”AppDelegate中调用applicationDidReceiveMemoryWarning:”);

}


2、视图控制器

在视图控制器中接收内存警告消息,需要重写didReceiveMemoryWarning方法。ViewController的代码片段:

- (void)didReceiveMemoryWarning

NSLog(@”ViewController中didReceiveMemoryWarning调用”);

[super didReceiveMemoryWarning];

//释放成员变量

[_listTeams release];

}


注意释放资源代码应该放在[super didReceiveMemoryWarning]语句下面。

3、其它类

在其它类中可以使用通知,在内存警告时候iOS系统会发出UIApplicationDidReceiveMemoryWarningNotification通知,凡是在通知中心注册了

UIApplicationDidReceiveMemoryWarningNotification通知的类都会接收到内存警告通知。ViewController的代码片段:

- (void)viewDidLoad

[super viewDidLoad];

NSBundle *bundle = [NSBundle mainBundle];

NSString *plistPath = [bundle pathForResource:@"team"

ofType:@"plist"];

//获取属性列表文件中的全部数据

NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];

self.listTeams = array;

[array release];

 //接收内存警告通知,调用handleMemoryWarning方法处理

 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

 [center addObserver:self

 selector:@selector(handleMemoryWarning)

 name:UIApplicationDidReceiveMemoryWarningNotification

 object:nil];

//处理内存警告

-(void) handleMemoryWarning

 NSLog(@”ViewController中handleMemoryWarning调用“);

}

我们在viewDidLoad方法中注册UIApplicationDidReceiveMemoryWarningNotification消息,接收到报警信息调用handleMemoryWarning方法。这些代码完全可以写在其它类中,在ViewController中重写didReceiveMemoryWarning方法就可以了,本例这是示意性介绍一下UIApplicationDidReceiveMemoryWarningNotification报警消息。

内存警告在设备上出现并不是经常的,一般我们没有办法模拟,但模拟器上有一个功能可以模拟内存警告,启动模拟器,选择模拟器菜单硬件→模拟内存警告,这个时候我们会在输出窗口中看到内存警告发生了。

2012-11-06 16:49:16.419 RespondMemoryWarningSample[38236:c07] Received memory warning.

2012-11-06 16:49:16.422 RespondMemoryWarningSample[38236:c07] AppDelegate中调用applicationDidReceiveMemoryWarning:

2012-11-06 16:49:16.422 RespondMemoryWarningSample[38236:c07] ViewController中handleMemoryWarning调用

2012-11-06 16:49:16.423 RespondMemoryWarningSample[38236:c07] ViewController中didReceiveMemoryWarning调用


[ios开发]-APP-上架流程 由于苹果的机制,在非越狱机器上安装必须通过官方的Appstore, 开发者开发好应用后上传Appstore,也需要通过审核等环节。 AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传Appstore作了支持。 本文从三个流程来介绍如何实现AppCan在 线编译出ipa包,以及上传到苹果Appstore。
关东升 国内著名iOS/Cocos技术作家,iOS技术顾问,Cocos最有价值专家(CVP),智捷课堂首席培训专家。担任51CTO社区iOS技术顾问。 著有多部移动开发畅销书。