通知中心(loading...)

1. 添加通知中心时不要忘记写dealloc(即便是ARC)

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 添加通知中心(不要忘记写dealloc)

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationClick:) name:@"friend" object:nil];

}



- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}


假如一个button 添加了 nameButtonClick方法 点击就会触发通知中心

- (void)nameButtonClick:(NSNotification *)notification
{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"friend" object:self userInfo:nil];


    CGRect rect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];// 键盘结束时候的rect

}



2. 给self.mainView 添加一个监听者 KeyPath:监听frame这个属性 options:监听新值的改变

[self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];


self.mainView frame 改变时会调用

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context



猜你喜欢

转载自blog.csdn.net/Michael_234198652/article/details/50809974