开发者

How to Notify View Controller that a Property of an Object has Changed

开发者 https://www.devze.com 2022-12-20 15:50 出处:网络
I have a view controller which gets an NSObject conforming to a开发者_运维百科 protocol and then displays a view with the object\'s properties.

I have a view controller which gets an NSObject conforming to a开发者_运维百科 protocol and then displays a view with the object's properties.

The question : How can my controller know that a property of this object has been modified and then refresh the view ?

Thanks a lot

Thierry


There are three ways of doing this:

  1. Have the object call a method in the controller in response to an event e.g. a user clicking the button. This is usually done using an IBAction.
  2. Set the controller to be the delegate of object e.g. a UIWebView sends a message to its delegate when it finishes loading a page.
  3. Use a notification. The object generates the notification and then one or more objects (including the controller) registers to listen for the notification. This is usually not used with interface elements although it can be.

I can't tell you more without more detail about the specifics of your project.


Your viewcontroller should conform to your . In your model, all your set methods should trigger appropriate functions you define in your modelchangedprotocol. This OO design pattern is also known as "Observer" design pattern.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号