Skip to Content
COCOA學習手冊--使用OBJECTIVE-C第三版
book

COCOA學習手冊--使用OBJECTIVE-C第三版

by Paris Buttfield-Addison, Jonathon Manning
June 2013
Intermediate to advanced
360 pages
6h 50m
Chinese
GoTop Information, Inc.
Content preview from COCOA學習手冊--使用OBJECTIVE-C第三版
148
|
第九章
鍵值觀察
考慮下列常見情節,你的螢幕上有視圖—譬如說,裡頭有一個文字欄位,展示從模型擷
取的資訊。在模型—視圖—控制器之設計模式中,控制器負責掌握模型裡的資訊何時改
變,並且指示視圖更新它的展示內容,以便反映這項事實。然而,控制器怎麼知道何時
應該更新視圖呢?
有兩種選項可供選擇︰反覆不斷地檢查模型,看看是否有任何東西被改變,或者等待模
型通知控制器改變已經發生的事實。第一個選項是最容易實作的—建立定期從模型擷取
最新值的定時器。不過,這種技術的問題是太浪費、不經濟—假如模型不常改變,大
部分的更新活動都只是多餘的,這會浪費時間與 CPU 資源。在由電池供應電力的裝置
上,不必要地恣意使用 CPU 只會浪費電池的電力。
為了解決這個問題,Cocoa 提供一種稱作
鍵值觀察
key-value observing
)的機制。鍵值
觀察允許某個物件事先註冊,以便在另一個物件改變它的某個特性值時接獲通知。在前
述情節中,控制器會要求模型物件在資料產生改變時發出通知;當控制器收到來自模型
的訊息時,視圖隨即被更新。這樣做讓更新次數減到最少。
鍵值觀察機制幫助簡化註冊通知,以及通知任何需要被告知改變發生之物件的流程。任
何物件上的任何特性都可以被觀察,只要該特性的名稱符合鍵值編程的要求即可。
註冊變更通知
當你註冊要在改變發生當下接獲通知時,你告訴該物件你希望觀察三件事︰特性改變時
要被通知的物件、要被觀察的特性名稱、以及改變發生時觀察者應該被告知的相關資
訊。取決於你的需求,你也可以包含在特性值發生改變時要傳送給欲執行之方法的指標 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

網頁互動式資料視覺化:使用D3

網頁互動式資料視覺化:使用D3

Scott Murray
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
自然语言处理实战:从入门到项目实践

自然语言处理实战:从入门到项目实践

Sowmya Vajjala, Bodhisattwa Majumder, Anuj Gupta, Harshit Surana

Publisher Resources

ISBN: 9789862768136