1.目的在于通过第二个页面,改变用户的在线状态(在第一个页面显示),通过传指针和回调的方法来解决这个问题
首先,到网上了解了一些回调的基本概念和机制, IOS回调机制——代理,通知中心以及Block
这里我使用回调。
1)直接用公司已经定义过的宏
typedef void (^OatosBaseBlock) (id data);
2)在.m文件里面加入块
{
OatosBaseBlock _complateBlock;
}
在.h文件里面添加属性以及回调函数,用指针来传递
@property (nonatomic, strong) NSString *onlineStatus;//传递状态
@property (nonatomic, strong) UIImage *icon;//传递状态头像
- (void)setStatusComplete:(OatosBaseBlock)block;//回调函数
3)回调函数
- (void)setStatusComplete:(OatosBaseBlock)block
{
_complateBlock = block;
}
//返回到上一个页面,通过动画的方式,并回调
-(void)onBtnBackClick:(id)sender
{
[UIView animateWithDuration:0.3 animations:^{
CGRect oldRect = self.view.frame;
oldRect.origin.x += oldRect.size.width;
self.view.frame = oldRect;
} completion:^(BOOL finished) {
if(_complateBlock)
{
_complateBlock(self);
}
}];
}
4)在上一个页面里面回调
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 0) {
if(indexPath.row == 0)
{
PadPersonalStateSettingViewController *perSettingVC = [[PadPersonalStateSettingViewController alloc]init];
perSettingVC.icon = self.icon;
perSettingVC.onlineStatus = [self.info objectForKey:@"onlineStatus"];
__weak id weakSelf = self;
[perSettingVC setStatusComplete:^(id data) {
PadPersonalStateSettingViewController *vctr = (PadPersonalStateSettingViewController *)data;
[weakSelf updateIConStatus:vctr.icon status:vctr.onlineStatus];
[vctr.view removeFromSuperview];
}];
[self pushInfoViewController:perSettingVC];
}
}
}
//通过这个函数来更新页面的图片和状态(传递指针的方式)
- (void)updateIConStatus:(UIImage *)img status:(NSString *)status
{
self.iconView.image = img;
self.icon = img;
UIImage *statusImage = [ImageUtils padGetImageNameByStatus:status];
self.statusView.imageView.image = statusImage;
[self.info setObject:status forKey:@"onlineStatus"];
}
总结:对于IOS的回调,需要多加练习,是一个好掌握和非常基本以及重要的技能。加油!