delegate,block,notification 傳值

Song Hye Kyo

delegate是經(jīng)典設(shè)計(jì)模式也就是大部分的語言都可以實(shí)現(xiàn)的模式,delegate只是保存了一個(gè)對(duì)象指針癣朗,直接回調(diào),沒有額外消耗.assign和weak區(qū)別
MRC @property (nonatomic, assign) id <MyDelegate> delegate;
ARC @property (nonatomic, weak) id <MyDelegate> delegate;
block出棧需要將使用的數(shù)據(jù)從椡眨拷貝到堆旷余,當(dāng)然對(duì)象的話就是加計(jì)數(shù),使用完或者block置nil后才消除扁达。所以我們用block時(shí)要進(jìn)行弱引用:
ARC下:weak typeof(self) weakSelf = self;
非ARC下:block typeof(self) weakSelf = self;
notification 通知的用法相對(duì)就是比較簡(jiǎn)單的,注意添加和刪除通知正卧;
iOS屬性中常用修飾詞的總結(jié)

Delegate

Class A
A.h
@protocol TestViewDelegate <NSObject>
-(void)selectedString:(NSString *)string;
@end
@property(weak, nonatomic) id <TestViewDelegate> testViewDelegate;
A.m
- (IBAction)back:(id)sender {
    if (self.testViewDelegate && [self.testViewDelegate respondsToSelector:@selector(selectedString:)]) {
        [self.testViewDelegate selectedString:@"T - T - Delegate"];
    }
}

Class B
#pragma mark - Delegate
- (IBAction)buttonClick:(id)sender {
    DelegateViewController *vc = [[DelegateViewController alloc]init];
    vc.testViewDelegate = self;
    [self presentViewController:vc animated:YES completion:nil];
}
-(void)selectedString:(NSString *)string{
    [self dismissViewControllerAnimated:YES completion:nil];//返回上個(gè)頁面
    NSLog(@"string-- >%@",string);
}

Block

Class A
A.h
typedef void (^TestViewblock)(NSString *string);
@interface BlockViewController : UIViewController
@property(nonatomic,strong)TestViewblock testViewBlock;
@end
A.m
- (IBAction)back:(id)sender {
    if (_testViewBlock) {
        _testViewBlock(@"T - T - Block");
    }
}

Class B
#pragma mark - block
- (IBAction)BlockClick:(id)sender {
    BlockViewController *vc = [[BlockViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
    
    __weak typeof(self) weakSelf=self;//避免block 循環(huán)緩存
    vc.testViewBlock=^(NSString *string){
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
        NSLog(@"string-->%@",string);
    };
}

Notification

- (IBAction)back:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test_notification" object:@"T - T - Notification"];
    [self dismissViewControllerAnimated:YES completion:nil];
}
---------------------------------------
#pragma mark - Notification
- (IBAction)NotificationClick:(id)sender
{
    NotificationViewController *vc = [[NotificationViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];//移除通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"test_notification" object:nil];//添加通知
}
-(void)notification:(NSNotification *)notification{
    NSString *sting = [notification object];
    NSLog(@"sting --->%@",sting);
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

Github代碼鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市跪解,隨后出現(xiàn)的幾起案子炉旷,更是在濱河造成了極大的恐慌,老刑警劉巖叉讥,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件窘行,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡图仓,警方通過查閱死者的電腦和手機(jī)罐盔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來救崔,“玉大人惶看,你說我怎么就攤上這事捏顺。” “怎么了纬黎?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵幅骄,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我本今,道長(zhǎng)拆座,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任诈泼,我火速辦了婚禮懂拾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘铐达。我一直安慰自己岖赋,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布瓮孙。 她就那樣靜靜地躺著唐断,像睡著了一般。 火紅的嫁衣襯著肌膚如雪杭抠。 梳的紋絲不亂的頭發(fā)上脸甘,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音偏灿,去河邊找鬼丹诀。 笑死,一個(gè)胖子當(dāng)著我的面吹牛翁垂,可吹牛的內(nèi)容都是我干的铆遭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼沿猜,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼枚荣!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起啼肩,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤橄妆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后祈坠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體害碾,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年赦拘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了慌随。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡另绩,死狀恐怖儒陨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情笋籽,我是刑警寧澤蹦漠,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站车海,受9級(jí)特大地震影響笛园,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜侍芝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一研铆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧州叠,春花似錦棵红、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至致板,卻和暖如春交煞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背斟或。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工素征, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人萝挤。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓御毅,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親平斩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子亚享,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容