ios中消息中心与协议的区别

// 注册消息中心
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getMessage:) name:@"MSHFOREVER" object:nil];

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(210, 143, 90);">#pragma mark - </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(210, 143, 90);">#pragma mark <span style="font-family: 'Heiti SC Light';">消息中心接收消息方法</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 255, 255);">- (<span style="color: #c2349b">void</span>)getMessage:(<span style="color: #c2349b">id</span>)obj</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 255, 255);">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(147, 200, 106);"><span style="color: #ffffff">    </span>BBSViewController<span style="color: #ffffff"> *bbs = [</span>BBSViewController<span style="color: #ffffff"> </span>getBBSViewController<span style="color: #ffffff">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(76, 191, 87);">//    NSString *needUrl = (NSString *)obj;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 175, 202);"><span style="color: #ffffff">    [</span><span style="color: #c2349b">self</span><span style="color: #ffffff">.</span>navigationController<span style="color: #ffffff"> </span>pushViewController<span style="color: #ffffff">:bbs </span>animated<span style="color: #ffffff">:</span><span style="color: #c2349b">YES</span><span style="color: #ffffff">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 255, 255);">}</p>// 这只是简单示例一下消息中心的注册以及接收,带上参数后进行接收会更高效的体现出垮多层层级关系的界面间进行传值
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(76, 191, 87);"><span style="font-family: Menlo;">// </span>跳转到下一个界面</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 255, 255);">- (<span style="color: #c2349b">void</span>)tableView:(<span style="color: #00afca">UITableView</span> *)tableView didSelectRowAtIndexPath:(<span style="color: #00afca">NSIndexPath</span> *)indexPath</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 255, 255);">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 175, 202);"><span style="color: #ffffff">    </span>NSNumber<span style="color: #ffffff"> *carId = [[[[</span><span style="color: #93c86a">_carInfoList</span><span style="color: #ffffff"> </span>objectAtIndex<span style="color: #ffffff">:indexPath.</span>section<span style="color: #ffffff">] </span>objectForKey<span style="color: #ffffff">:</span><span style="color: #e44448">@"serieslist"</span><span style="color: #ffffff">] </span>objectAtIndex<span style="color: #ffffff">:indexPath.</span>row<span style="color: #ffffff">] </span>objectForKey<span style="color: #ffffff">:</span><span style="color: #e44448">@"id"</span><span style="color: #ffffff">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(228, 68, 72);"><span style="color: #ffffff">    </span><span style="color: #00afca">NSString</span><span style="color: #ffffff"> *moreUrl = [</span><span style="color: #00afca">NSString</span><span style="color: #ffffff"> </span><span style="color: #00afca">stringWithFormat</span><span style="color: #ffffff">:</span>@"http://app.api.autohome.com.cn/autov4.2/cars/seriessummary-a2-pm1-v4.2.0-s%@-t-c120100.html"<span style="color: #ffffff">, carId];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 175, 202);"><span style="color: #ffffff">    [[</span>NSNotificationCenter<span style="color: #ffffff"> </span>defaultCenter<span style="color: #ffffff">] </span>postNotificationName<span style="color: #ffffff">:</span><span style="color: #e44448">@"MSHFOREVER"</span><span style="color: #ffffff"> </span>object<span style="color: #ffffff">:moreUrl];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 255, 255); min-height: 13px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(255, 255, 255);">}</p>

最近在临摹“汽车之家”的APP,在写“找车”界面时发现会有tableView无法跳转到新的UIViewController的现象,因为无论push也好,还是模态进入也罢,都是UIViewController之间的交互方法,对于UITableView很是无奈,当两个交互界面之间的中间层过多时将无法进行有效的跳转,经过一番折腾,想到了运用消息中心进行调用主要的UIViewController进行有效的界面之间的交互跳转。同时,涉及UITableView的cell点击事件是跳转到同一个view进行不同数据的操作,再次对于跳转到的view进行了一次单例操作,这样既可以在任意界面import头文件之后进行高效非重复的调用。

其实消息中心跟协议都是很不错的传值方法以及回调,小心中心相对来说比协议的使用更加灵活,只需要注册一个消息中心,其他任何界面只要收听即可,而对于协议就稍显复杂和冗余。

另外,在代码的机动性,灵活性,以及后续代码维护上来讲,消息中心更加简单,需要进行代码的维护以及更改时,消息中心的维护更加简单。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值