1记舆、注冊、移除Notification
????????- (void)viewWillAppear:(BOOL)animated {
? ????????? ????????[super viewWillAppear:animated];
? ????????? ????????[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil];
????????}
????????- (void)viewWillDisappear:(BOOL)animated {
? ????????? ????????[super viewWillDisappear:animated];
? ????????? ????????[[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil];
? ? ? ? ? ?}
????????????就是在頁面出現(xiàn)的時候注冊通知呼巴,頁面消失時移除通知泽腮。你這邊可要注意了,一定要成雙成對出現(xiàn)衣赶,通過指定的 name 移除通知盛正,如果你只在viewWillAppear 中 addObserver沒有在viewWillDisappear 中 removeObserver那么當(dāng)消息發(fā)生的時候,你的方法會被調(diào)用多次屑埋。
2豪筝、在block中調(diào)用其它方法
改前:
????????__weak typeof(self)weakself = self;
????????[[BluetoothUtil sharedInstance] simpleGetBLEStateWithBlock:^(NSString *stateString) ?{
????????????????dispatch_async(dispatch_get_main_queue(), ^{? ? ? ? ? ?
????????????????????????[weakself BLEStartScan];? ? ? ? ? ? ?
????????????????});
????????}];
????????- (void)BLEStartScan ?{
? ????????????????? __weak typeof(self) weakSelf = self;
????????}
????????block中調(diào)用self方法時需要把weaksekf也傳遞到方法中
????????改后:
????????__weak typeof(self)weakself = self;
????????[[BluetoothUtil sharedInstance] simpleGetBLEStateWithBlock:^(NSString *stateString) ?{
????????????????dispatch_async(dispatch_get_main_queue(), ^{? ? ? ? ? ?
????????????????????????[weakself BLEStartScan:weakself];? ? ? ? ? ? ?
?????????????????});
????????}];
????????- (void)BLEStartScan:(BindDeviceVC *)uself ?{
????????????????__weak typeof(self) weakSelf = uself;
????????}
3、在block中不能自己引用自己
????????[self.bridge registerHandler:@"unKnowTypeToQuestion" handler:^(id data, WVJBResponseCallback ????responseCallback) {
????????????????dispatch_async(dispatch_get_main_queue(), ^{
????????????????????????[WKWebVC showWithContro:self withUrlStr:homeUrl withTitle:title];
????????????????});
????????}];
????????在block中不能自己引用自己摘能,所以block中WKWebVC需要改為HJWebVC(隨意一個別的)续崖;
weakself問題,不能使用self
????????改后:
????????[self.bridge registerHandler:@"unKnowTypeToQuestion" handler:^(id data, WVJBResponseCallback responseCallback) {
????????????????dispatch_async(dispatch_get_main_queue(), ^{
????????????????????????[HJWebVC showWithContro:weakself withUrlStr:homeUrl withTitle:title];
????????????????});
????????}];
4团搞、__weak和__block
????????在cellForRowAtIndexPath中:
????????改前:
????????__block CommentInBelieveCell *cellBlock = cell;
????????cell.zanBlock = ^{
????????};
????????__weak使用有問題
????????__weak和__block
????????__weak 主要用于防止block中的循環(huán)引用
????????__block用于修飾某些block內(nèi)部將要修改的外部變量
????????改后:
????????_weak CommentInBelieveCell *cellBlock = cell;
????????cell.zanBlock = ^{
????????}严望;
5、cellForItemAtIndexPath中block中使用cell
????????- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
????????????????cell.buttonBlock = ^{
????????????????????????[collectionView reloadData];
?????????????????};
????????}
????????其中cell和collectionView循環(huán)引用
????????改后:
????????- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
?????????????????__weak typeof(self)weakSelf = self;
????????????????cell.buttonBlock = ^{
????????????????????????[weakSelf.collectionView reloadData];
????????????????};
????????}
????????- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
????????????????cell.selectBlock = ^{
????????????????????????[tableView reloadData];
????????????????};
????????}
????????其中cell和tableView循環(huán)引用
????????改后:
????????- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
????????????????__weak typeof(self)weakSelf = self;
? ? ? ? ? ? ? ? ?cell.selectBlock = ^{
? ? ? ? ? ? ? ? ? ? ? ? ? ?[weakSelf.tableView reloadData];
?????????????????};
? ? ? ? ? ?}
6逻恐、block中使用項(xiàng)目中自定義的log
????????#define STLog(...) NSLog(@"%s(%p) %@", __PRETTY_FUNCTION__, self, [NSString stringWithFormat:__VA_ARGS__])
????????block和self循環(huán)調(diào)用
????????block中不使用STLog像吻,必須使用觀察數(shù)據(jù)的話使用NSLog
7峻黍、AFNetworking中,manage接收到請求結(jié)果后需要
????????__weak typeof(manager) weakManager = manager;
????????[weakManager invalidateSessionCancelingTasks:YES resetSession:YES];