简体   繁体   English

iPhone应用程序开发的内存问题

[英]Memory issue with iphone app development

I am developing an iphone application which uses very simple interface and does database handling at the backend. 我正在开发一个使用非常简单的界面并在后端进行数据库处理的iphone应用程序。 I am enabling ARC OPTION as well. 我也启用了ARC OPTION。 My viewDidLoad method is as follows: 我的viewDidLoad方法如下:

    - (void)viewDidLoad
   {
    [super viewDidLoad];
// Do any additional setup after loading the view from its nib.

/*********needed to implement scroll view********/
svScroll.frame = CGRectMake(0, 0, 320, 460);
svScroll.contentSize = CGSizeMake(320, 800);
/*********************************************/

//[DataHelper openDbCompany];

NSString *date=[DataHelper getFinYr];

[btDate setTitle:[DataHelper dateSqliteToNormal:date] forState:UIControlStateNormal];

arrayUnitsMeasure=[[NSMutableArray alloc]initWithArray:[DataHelper getUnitsOfMeasure]];

//[DataHelper closeDbCompany];

tfValue.keyboardType=UIKeyboardTypeDecimalPad;
tfQuantity.keyboardType=UIKeyboardTypeDecimalPad;
tfCostUnit.keyboardType=UIKeyboardTypeDecimalPad;

//catching the notification for text field value change.
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:tfQuantity];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:tfCostUnit];

    }

My .h file contains contains IBOutlets which are defined as follows: 我的.h文件包含包含IBOutlet,它们的定义如下:

    @interface
     Create_Inventory_Item:UIViewController<Date_Picker_Protocol,Picker_View_Protocol,UITextFieldDelegate>
    {
        IBOutlet UIScrollView *svScroll;
        IBOutlet UITextField *tfItemName;

IBOutlet UILabel *lbUnitsOfMeasure;
IBOutlet UIButton *btSelectUnitsMeasure;

IBOutlet UIButton *btDate;
IBOutlet UINavigationBar *btBack;

IBOutlet UITextField *tfQuantity;
IBOutlet UITextField *tfCostUnit;
IBOutlet UITextField *tfValue;

IBOutlet UIButton *btCreate;
NSMutableArray *arrayUnitsMeasure;

UIButton *btKeyboardDone;
UIView *accessoryView;
UITextField *txtActiveField;
UIButton *btMinus;
Picker_View *callPickerView;
Date_Picker *callDatePicker;
    }

    @property(nonatomic,retain) UIButton *btMinus;
    @property(nonatomic,retain)UITextField *txtActiveField;
    @property(nonatomic,retain) UIButton *btKeyboardDone;
    @property(nonatomic,retain)UIView *accessoryView;
    @property(nonatomic,retain) IBOutlet UINavigationBar *btBack;
    @property(nonatomic,retain)IBOutlet UIScrollView *svScroll;
    @property(nonatomic,retain)IBOutlet UITextField *tfItemName;
    @property(nonatomic,retain)IBOutlet UILabel *lbUnitsOfMeasure;
    @property(nonatomic,retain)IBOutlet UIButton *btSelectUnitsMeasure;
    @property(nonatomic,retain) IBOutlet UIButton *btDate;
    @property(nonatomic,retain) IBOutlet UITextField *tfQuantity;
    @property(nonatomic,retain) IBOutlet UITextField *tfCostUnit;
    @property(nonatomic,retain)IBOutlet UITextField *tfValue;
    @property(nonatomic,retain) IBOutlet UIButton *btCreate;

    -(IBAction)btSelectUnitsMeasure:(id)sender;
    -(IBAction)btDate:(id)sender;
    -(IBAction)btCreate:(id)sender;
    -(IBAction) hideKeyboard:(id)sender;
    -(IBAction)showAlerView:(NSString *)message;
    -(IBAction)btBack:(id)sender;

Please tell me what do I need to do in dealloc and viewDidUnloadMethod? 请告诉我在dealloc和viewDidUnloadMethod中需要做什么? I am using ARC OPTION. 我正在使用ARC OPTION。
Also, when I run the app with profile option in simulator for memory allocation and leak, it sometimes shows MEMORY LEVEL LOW WARNING and MEMORY LEVEL NORMAL. 另外,当我在模拟器中使用配置文件选项运行应用程序以进行内存分配和泄漏时,有时会显示MEMORY LEVEL LOW WARNING和MEMORY LEVEL NORMAL。 What is the cause of this? 这是什么原因?

If you're using ARC you just need a dealloc that nullifies all the object-based members in that class instance. 如果使用的是ARC,则只需要取消分配即可使该类实例中所有基于对象的成员无效。

All apps get memory warnings once in a while. 所有应用程序偶尔都会收到内存警告。 You can choose to respond by nullifying members that can be lazy-initialized later. 您可以选择通过使以后可以延迟初始化的成员无效来进行响应。

In my apps I lazy-initialize most visual members (UIViews etc...) in viewWillAppear and aggressively release in the viewDidDisappear method. 在我的应用中,我在viewWillAppear中延迟初始化了大多数可视成员(UIViews等),并在viewDidDisappear方法中积极释放。 This way only 2 views can have their members initialized at once (during view controller transitions) and only 1 view when that view is the only visible one. 这样,只有2个视图可以一次初始化其成员(在视图控制器转换期间),而只有1个视图是唯一可见的视图。

As a result I get very few memory warnings, except for when manipulating big images and such. 结果,除了处理大型图像之类的信息时,我几乎没有收到任何内存警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM