【三】iOS面试问题

一、观察者模式(通知机制,KVO机制)

观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。 简而言之,就是A和B,A对B的变化感兴趣,就注册A为观察者,当B发生变化时通知A,告知B发生了变化。这个也叫做经典观察者模式。

ViewController.m

#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property(nonatomic,strong)Person *child;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.child = [[Person alloc]init];
    NSLog(@"%d",self.child.age);

    [self.child addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"aaa"];
    [self.child addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"bb"];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];
    btn.backgroundColor = [UIColor orangeColor];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

//监听回调
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"监听回调");
    NSLog(@"%@,%@,%@",object,change,context);
    NSLog(@"新的:%@",[change objectForKey:@"new"]);
}

-(void)click
{
    [self.child changeAgeValue:100 :@"张三"];
}

-(void)dealloc
{
    [self removeObserver:self.child forKeyPath:@"age"];
}

Person.m

-(instancetype)init
{
    self = [super init];
    if (self)
    {
        self.age = 10;
        self.name=@"张三";
    }
    return self;
}

-(void)changeAgeValue:(int)number :(NSString *)newName
{
    self.age = number;
    self.name=newName;
}


//监听回调
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"%@,%@,%@",object,change,context);
}

Person.h

@interface Person : NSObject
@property(nonatomic,assign)int age;
@property(nonatomic,strong)NSString *name;


-(void)changeAgeValue:(int)number :(NSString *)newName;

@end

二、代理模式

代理模式是一种消息传递方式,一个完整的代理模式包括:委托对象、代理对象和协议。
协议:用来指定代理双方可以做什么,必须做什么。
委托对象:根据协议指定代理对象需要完成的事,即调用协议中的方法。
代理对象:根据协议实现委托方需要完成的事,即实现协议中的方法。

实例:
ViewController.m

#import "ViewController.h"
#import "TimerControl.h"

@interface ViewController ()<UpdateAlertDelegate>

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TimerControl *timer = [[TimerControl alloc] init];
    timer.delegate = self; //设置代理实例
    NSLog(@"调用TimerController");
    [timer startTheTimer];//启动定时器,定时5触发  //=====1
}
 
- (void)didReceiveMemoryWarning
{
    NSLog(@"销毁");
    [super didReceiveMemoryWarning]; //=====5
    // Dispose of any resources that can be recreated.
}
 
//"被代理对象"实现协议声明的方法,由"代理对象"调用
- (void)updateAlert:(NSString *)text
{
//    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
//
//    alert.alertViewStyle=UIAlertViewStyleDefault;
//    [alert show];
    NSLog(@"回到ViewController,%@",text); //=====4
}

TimerControl.h

#import "TimerControl.h"

@implementation TimerControl
 
 
- (void) startTheTimer
{
    NSLog(@"设置计时器");
    [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(timerProc) userInfo:nil repeats:NO];//=====2
}
 
- (void) timerProc
{
    //当代理响应updateAlert的方法时,让其执行。 不安全
    if (_delegate && [_delegate respondsToSelector:@selector(updateAlert:)]) {
        NSLog(@"TimerControl处理");
        [self.delegate updateAlert:@"你好呀"];//代理更新UI //=====3
    }
   
}
 
@end

TimerControl.m

#import <Foundation/Foundation.h>
 
 
//协议定义
@protocol UpdateAlertDelegate <NSObject>
- (void)updateAlert:(NSString *)text;
@end
 //UpdateAlertDelegate协议名
 
@interface TimerControl : NSObject
//遵循协议的一个代理变量定义
@property (nonatomic, weak) id<UpdateAlertDelegate> delegate;
 
- (void) startTheTimer;
   
@end

此时TimerControl就是委托对象、ViewController就是代理对象、UpdateAlertDelegate就是协议。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值