IOS中通知中心的使用(傳值)

通知中心可以實現(xiàn)從后一個界面向前一個界面?zhèn)髦档墓δ堋#ㄟ@里認(rèn)為是從b界面向a界面?zhèn)髦担?/p>

第三個界面的代碼

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@(downLoadProgress),@"progress",progressStr,@"progressStr",@(totalBytesExpectedToWrite),@"totalProgress", nil];

[[NSNotificationCenter defaultCenter] postNotificationName:@"ViewController" object:@"zhangsan" userInfo:dic];

從b界面發(fā)送一條通知,其中Name為第一個界面的名稱,object為要傳遞的值,如果使用單值傳遞,只需將所要傳的值賦給object即可,如果要使用多值傳遞,可以將要傳遞的值放入一個字典,將值傳遞過去,object除了可以傳遞值之外,還可以作為標(biāo)識符。

第一個界面的代碼

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(receiveNotificiation:) name:@"ViewController" object:@"zhangsan"];

//通知中心的回調(diào)方法

- (void)receiveNotificiation:(NSNotification*)sender{

dispatch_async(dispatch_get_main_queue(), ^{

double progress = [[sender.userInfo objectForKey:@"progress"] doubleValue];

self.downLoadProgressView.progress = progress;

self.currentProgress_label.text = [sender.userInfo objectForKey:@"progressStr"];

});

}

//將通知中心移除

- (void)dealloc{

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ViewController" object:@"zhangsan"];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容