如何实现memoryReceiveWarning

时间:2013-12-26 13:49:45

标签: ios iphone uiview uiscrollview didreceivememorywarning

我处于开发的初始阶段,我想确保我在内存管理方面的方向正确。

我有一个名为LayoutViewController的视图控制器,带有xib.I有一个自定义ui子类,其xib名为LayoutContainerView,它基本上包含一个scrollview。我在IBOutlet的LayoutViewController xib中使用LayoutContainerView。

我有另一个UIView子类,它包含一个带有背景图像的视图,一些标签和一个与viw相同框架的透明按钮。我在LayoutContainerView的scrollview中添加了这个自定义控件。

我的视图控制器.h看起来像这样:

#import <UIKit/UIKit.h>

@protocol LayoutVcRemovedProtocol;

extern const char* MyConstantKey;
@interface LayoutViewController : UIViewController

// public properties
@property (nonatomic, copy) NSString *databasePath;


@property (nonatomic, weak) id<LayoutVcRemovedProtocol> layoutVcRemovedProtocolDelegate;

@end

@protocol LayoutVcRemovedProtocol<NSObject>

-(void) layoutVcRemovedProtocolMethod;

@end
=========================


**some of relevant code of **implementation** file looks like this:**

 //private stuffs goes here
const char* MyConstantKey = "MyConstantKey";
@interface LayoutViewController () <UIActionSheetDelegate, UIAlertViewDelegate>
@property(nonatomic, weak) IBOutlet LayoutContainerView *layoutContainerView;
@property(nonatomic, weak) IBOutlet UIButton *backbutton;
@property(nonatomic, weak) IBOutlet UILabel *layoutNameLabel;


@property (nonatomic, strong) UIView *baseView;

@property (nonatomic, strong) NSMutableArray *layoutModelArray;


-(IBAction)backButtonPressed;

@end

@implementation LayoutViewController

//my viewDidLoad and viewWillAppear look like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self adjustLayoutContainerFrameAndSetDataBasePath];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.layoutNameLabel.text = [Utils getLayoutName];
    [self getLatestData];
}

-(void) getLatestData
{
    [self setUpDataSource];
    [self setUpComponentsOnLayoutScreen];
}

#pragma mark - datasource method
-(void)setUpDataSource
{`
    self.layoutModelArray = (NSMutableArray *)[LAYOUTMODULE getAllLayoutData];
}`



-(void)setUpComponentsOnLayoutScreen
{`
    for (int i = 0; i < self.layoutModelArray.count; i++)
    {
        Layout *layout = [self.layoutModelArray objectAtIndex:i];

        [self drawViewWithLayoutObject:layout];
    }

    [self.layoutContainerView.scrollView adjustContentSize];
}

这就是我想要管理记忆的内容:

-(void) cleanLayoutModelArray
{

    if (self.layoutModelArray != nil && self.layoutModelArray.count >0)
    {
        [self.layoutModelArray removeAllObjects];
    }

}

-(void) cleanComponents
{

    [self.layoutContainerView.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

}

//user events

-(void) placeOrderForLayout:(Layout *)layout
{
    [DELEGATE showLandscapeLoading];
    //web service COMMUNICATION HERE  here
    OrderModule *oModule = [OrderModule sharedModule];

     NSDictionary *requestDictionary = [NSDictionary dictionaryWithObjectsAndKeys:oModule.currentOrder.mOrderId,@"order_id",oModule.currentOrder.mPosId,@"pos_id",[Utils currentDate],@"book_date", layout.componentId, @"component_id", nil];



    BOOL status = [LAYOUTMODULE placeComponentOrderThroughAPI:requestDictionary];

    if (status == TRUE)
    {
        [self performCleanUp];
        [self getLatestData];
    }
    [DELEGATE stopLandscapeLoading];
}

帮助我或建议:

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

我在memoryWarningDelegate视图控制器中尝试的内容变为黑屏。

1 个答案:

答案 0 :(得分:0)

你的代码中有很多@properties标记为“弱” - 当你将事物标记为弱时,这意味着其他东西正在追踪它是否仍然存在。在这里,您正在使用IBOutlet项目,您的控制器应该跟踪它们,因此它们应该标记为“强”,而不是弱。我会在这里回顾你对'弱'的所有用法。另请参阅Apple关于内存管理的优秀文档。 ARC将为您处理大部分内存管理。通常,你需要在didReceiveMemoryWarning中做的唯一事情是设置nil是一个大对象的任何东西,比如你可以在用户再次需要时重新加载的视频或网页。通常,在您的典型视图中,此时您可以释放的空间并不多。另请注意,iOS设备目前拥有相当大的内存占用量,因此您不必担心只要有可能再次需要,小型数据结构仍会驻留在内存中。如果您遇到内存不足问题,我会使用仪器运行并检查是否存在泄漏。