iOS一些系统函数

1,prepareForReuse。

UITableViewCell的系统方法,用户可以自行重写,重写必须遵循系统的父类方法:

- (void)prepareForReuse {
    [super prepareForReuse];    
}

官方主要用途介绍:

Discussion
If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view'€™s delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.
如果一个UITableViewCell对象是可重用的 - 也就是说,它有一个重用标识符 - 这个方法在从UITableView方法dequeueReusableCellWithIdentifier:返回对象之前被调用。 出于性能考虑,您应该只重置与内容无关的单元格属性,例如,alpha,编辑和选择状态。 tableView:cellForRowAtIndexPath:中的表格视图委托应该在重新使用单元时始终重置所有内容。 如果单元对象没有关联的重用标识符,则不调用此方法。 如果您重写此方法,则必须确保调用超类实现。

个人理解就是当重用机制的cell在屏幕中消失之后,然后再出现调用的一个方法,一些特定的场合,展现Cell UI动画的时候可以用到。

2,load()

系统描述是每次添加当前类的时候调用:

Invoked whenever a class or category is added to the Objective-C runtime; implement this method to perform class-specific behavior upon loading.
每当将类或类别添加到Objective-C运行时时调用; 实施此方法以在加载时执行特定于类的行为。
The load message is sent to classes and categories that are both dynamically loaded and statically linked, but only if the newly loaded class or category implements a method that can respond.
 
The order of initialization is as follows:
 
All initializers in any framework you link to.
 
All +load methods in your image.
 
All C++ static initializers and C/C++ __attribute__(constructor) functions in your image.
 
All initializers in frameworks that link to you.
 
In addition:
 
A class’s +load method is called after all of its superclasses’ +load methods.
 
A category +load method is called after the class’s own +load method.
 
In a custom implementation of load you can therefore safely message other unrelated classes from the same image, but any load methods implemented by those classes may not have run yet.

系统的描述是,将当前项目当做一个镜像,当镜像加载所有文件的时候就会调用每一个文件中的load方法。如果当前文件的父类中也有此方法,那么先加载父类中的load方法,然后加载子类中的load方法。此方法可以在初始化一些static变量的时候进行调用,此方法在当前镜像中只运行一次。

+ (void)load {
//    Method method1 = class_getClassMethod([self class], @selector(method_first));
//    Method method2 = class_getClassMethod([self class], @selector(method_second));
    
    Method method1 = class_getInstanceMethod([self class], @selector(method_first));
    Method method2 = class_getInstanceMethod([self class], @selector(method_second));
    
    method_exchangeImplementations(method1, method2);
    
}

3,initialize()

Initializes the class before it receives its first message.在收到第一条消息之前初始化该类。

当系统调用该类的时候加载的初始化方法,加载且只加载一次。例如ctrl调用了push方法,则下一个ctrl调用initialize方法,并且必须遵循其父类方法。

在初始化当前类的一些特定变量或者一些特定操作的时候可以调用当前方法。

load,initialize,init的加载顺序分别为:

+ (void)load{
    NSLog(@"%s - - - first",__func__);
}
+ (void)initialize{
    [super initialize];
    NSLog(@"%s %@ - - - second",__func__,[self class]);
}
- (instancetype)init{
    if (self = [super init]) {
        NSLog(@"%s - - - third",__func__);
    }
    return self;
}

4,viewDidLayoutSubviews

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    NSLog(@"%s",__func__);
}
Called to notify the view controller that its view has just laid out its subviews.
调用来通知视图控制器它的视图刚刚列出了它的子视图。
此方法是viewcontroller的方法,当控制器已经布局好了self.view上边的subview时候,则调用此方法。此方法必须遵循它的父类方法。整个controller的生命周期为:
viewDidLoad -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppear -> viewWillDisappear -> viewDidDisappear
//
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    NSLog(@"%s",__func__);
}
 
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    NSLog(@"%s",__func__);
}

运用场景一般都是当ctrl布局完view之后通过传递过来的某个值在此方法去改变subview的bound或者frame。

5,- (void)loadView

– (void)loadView自定义当前controller的self.view,当用户没有调用或者调用没有分配self.view的时候,系统会自动分配一个空view给当前ctrl的self.view,一般运用场景是当前controller中只有一个背景view的时候,可以节省一些资源,因为少去了系统分配的self.view。

- (void)loadView
{
    //CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)
    self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
}

6,- (instancetype)initWithCoder:(NSCoder *)aDecoder;

在xib或storyboard中写一个View,系统来完成反序列化工作的时候,此时会调用initWithCoder方法。也就是从文件加载UIView的时候,initWithCoder会执行。类似于纯代码的- (instancetype)initWithFrame:(CGRect)frame这个方法。

7,AFNetworking获取服务器返回的头信息内容;

NSHTTPURLResponse *httpURLResponse = (NSHTTPURLResponse*)task.response;
NSDictionary *allHeaderFieldsDict = httpURLResponse.allHeaderFields;
NSLog(@"%@",allHeaderFieldsDict);

8,保持手机屏幕长亮,yes:

[[UIApplication sharedApplication] setIdleTimerDisabled:NO]; 
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

9,当前view添加到/移除superview上之后触发的方法:

- (void)didMoveToSuperview

10,判断当前controller是否加载到内存中(是否显示):

viewLoaded
[viewController isViewLoaded];
此属性的值是YES视图在内存中时或NO不在内存中时的值。如果视图当前不在内存中,则访问此属性不会尝试加载视图。

Leave a Reply

Required fields are marked *