A 向 B 傳值:
A要傳值,就要告訴別人我可以傳值。
A 要聲明這個(gè)協(xié)議,定義遵守該協(xié)議的delegate屬性。
A本身并不關(guān)心數(shù)據(jù)傳給誰了,誰實(shí)現(xiàn)了協(xié)議的方法就把數(shù)據(jù)傳給誰。
A用protocol告訴別人我可以把數(shù)據(jù)傳出去后,先判斷[_delegate respondsToSelector]是否為真,若為真,則調(diào)用協(xié)議里的方法------->傳值
B要接受值,就要遵守這個(gè)協(xié)議,實(shí)現(xiàn)協(xié)議里規(guī)定的方法,將self賦值給A的delegate,實(shí)現(xiàn)協(xié)議的方法的同時(shí)------------------------->收值
-------------------------------------------------------------------------------------
嗯,當(dāng)然也可以用block來實(shí)現(xiàn)
A 向 B 傳值:
A要傳值,同樣也要在A中聲明這個(gè)block,并且調(diào)用這個(gè)block,將值作為參數(shù)傳出去。
B要收值,就要實(shí)現(xiàn)這個(gè)block,并且值已經(jīng)作為參數(shù)傳進(jìn)來可以直接使用。
但是會出現(xiàn)循環(huán)強(qiáng)應(yīng)用的情況,下邊是解決辦法:
- (void)btnClicked:(id)sender
{
? ? [self.navigationController pushViewController:_viewB animated:YES];
? ? __weak typeof(self) weakself = self;
? ? ?_viewB.showText = ^{
? ? ? ? ?__strong typeof(weakself) strongself = weakself;
? ? ? ? strongself->_btn.backgroundColor = [UIColor redColor];
? ? };
}