代理、单例、tableView必写的两个代理方法、block及setter,getter方法

代理、单例、tableView必写的两个代理方法、block及setter,getter方法_第1张图片


1.代理
(1)
@protocol ClassNameDelegate
@optional
方法名
@end

@interface
@property (nonatomic, assign) id delegate;  
@end

(2)
@protocol requestFinish
-(void) requestFinish:(id) sender;
-(void) requestError:(id) sender;
@end
@interface HttpRequest : NSObject
{
}
@property (nonatomic, assign) id requestDelegate;
@end
实现:
    if (_requestDelegate && [_requestDelegate respondsToSelector:@selector(requestFinish:)])
    {
        [_requestDelegate requestFinish:reciveData];
    }


2.单例

@interface
+ (instancetype)sharedClassName;
@end

@implementation
(instancetype)sharedClassName {
    static ClassName *className = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
            _className = [ClassName new];
    });
    
    return _className;
}
@end

3.tableView代理方法
@implementation
//     cell 每组几行:
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   }
//     cell 的内容:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {   }
@end



4.block
(1)
@interface ViewController : UIViewController
@property (nonatomic, copy) 返回值(^block名)(参数列表);    // 声明
@end

@implementation
self.block名(传参)   // 调用
@end

@implementation
self.block名 = ^(参数列表) {
    // 实现    
}  
@end

(2)block 新类型
typedef void(^ResultBlock) (int,int);
@interface ViewController: UIViewController
// 定义一个block属性,一定使用 copy 特性
@property (nonatomic,copy)ResultBlock block;
@end

@implementation
(void)value:(ResultBlock)block;
@end


5 setter方法
- (void)setName:(NSString *)name {
    _name = name;
}

6 getter方法
- (NSString *)name {
    return _name;
}


你可能感兴趣的:(手敲代码。面试必备,UI,代理,单例,block,setter和getter方法)