iOS开发笔记之二十三——一个delegate没有更新引起的bug(bug总结三)

一个delegate没有更新引起的bug

1、场景如下:
(1)有个mainFrameController,在这个mainFrameController点击对应彩虹tab切换分别加载另外3个子controller,分别为A-controller,B-controller,C-controller
          这3个controller实例通过GroupController类进行区别并统一初始化的;
(2)A、B、C三个controller分别要实现语音播报功能,所以都要实现Sound类中的SoundDelegate中的方法;
(3)A、B、C三个界面第一次加载时,在进行初始化时,都会将自己的类实例执行赋值给Sound类中的speechDelegate指针;
Sound类中:

@property (nonatomic, weak) id<SoundDelegate> speechDelegate;

GroupController类中定义的语音实例变量:

@property (nonatomic, retain) Sound *soundButton;

GroupController类中loadView方法:

- (void)loadView
{
    .....
    if (mContentType_e == A)
    {
        .....
        A *a = [[A alloc]init];
        self.soundButton.speechDelegate = a;
        .....
    }
    if (mContentType_e == B)
    {
        .....
        B *b = [[B alloc]init];
        self.soundButton.speechDelegate = b;
        .....
    }
    if (mContentType_e == C)
    {
        .....
        C *c = [[C alloc]init];
        self.soundButton.speechDelegate = c;
        .....
    }
    .....
}


(4)为了省去每次切换页面都要初始化,所以在mainFrameController中进行第一次初始化时,就进行持久化保存到一个字典中;
代码大致如下:

-(void) load_A
{
    .....
    GroupController * model = [self.modelDict objectForKey:@"A类"];
    if (model == nil)
    {
        model = [[GroupController alloc]initWithType:A_CONTENT      //在这里进行了model初始化,执行loadView
                                            ViewRect:[GroupController getMainViewContentFrame]];
        [self.modelDict setObject:model forKey:@"A类"];
    }
    .....
}

-(void)load_B
{
    .....
    GroupController * model = [self.modelDict objectForKey:@"B类"];
    if (model == nil)
    {
        model = [[GroupController alloc]initWithType:B_CONTENT
                                            ViewRect:[GroupController getMainViewContentFrame]];
        [self.modelDict setObject:model forKey:@"B类"];
    }
    .....
}

-(void) load_C
{
    .....
    GroupController * model = [self.modelDict objectForKey:@"C类"];
    if (model == nil)
    {
        model = [[GroupController alloc]initWithType:C_CONTENT
                                            ViewRect:[GroupController getMainViewContentFrame]];
        [self.modelDict setObject:model forKey:@"C类"];
    }
    .....
}

在这种场景下bug就出现了,先进入A界面进行语音播报,再第一次切换至B界面进行语音播报,再切换回A进行语音播报,发现竟还在播报B的内容;

2、原因分析:
语音播报speechDelegate只在界面第一次初始化时进行了赋值,A的第一次进入就执行了self.soundButton.speechDelegate = a;
再次B时执行了self.soundButton.speechDelegate = b;再切换回A时,由于已经进行了持久化处理,就不在进行初始化处理,就不在执行
self.soundButton.speechDelegate = a;导致speechDelegate始终是B的值;
3、参考解决方案
此问题主要是类实例的持久化保存导致speechDelegate没有更新导致,所以需要及时更新speechDelegate,让它可以调用对应的代理方法。
解决此bug采取在每次切换到某个界面时,对应的controller类中的viewWillAppear中更新speechDelegate;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    ......
    self.soundButton.speechDelegate = self;
    ......
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值