Key-value observing
Key-value observing
- Subject: Key-value observing
- From: Hend Kamal <email@hidden>
- Date: Wed, 04 May 2011 12:14:17 +0300
- Importance: Normal
Dear all,
I tried to use "Key-value observing" pattern "http://developer.apple.com/library/iOS/#documentation/General/Conceptual/Devpedia-CocoaApp/KVO.html"
at my iphone application but my problem is that the "ObserveValue"
method was invoked only at property initialization time but didn't feel
with any extra changes of it even I assign "NSKeyValueObservingOptions"
enum to New,Old and OldNew.
Here is part of my project:
I assume the scenario of person and bankAccount; BankAccount object must notify person if "AccountBalance" property changed.
Person class:
public class Person:NSObject
{
public Person () {}
public override void ObserveValue (NSString keyPath,
NSObject ofObject,NSDictionary change, IntPtr context) { //Do somthing
}
Bank account class:
public class BankAccount:NSObject
{
public BankAccount () {}
public override void SetValueForKey (NSObject value, NSString key)
{
WillChangeValue(key.ToString());
this.accountBalance=int.Parse(value.ToString());
DidChangeValue(key.ToString());
}
public void setAccountBalance(int accountBalance)
{this.accountBalance=accountBalance;}
public int getAccountBalance()
{return accountBalance;}
private int _accountBalane;
[Export ("accountBalance")]
public int accountBalance
{
get{return _accountBalane;}
set
{
WillChangeValue("accountBalance");
_accountBalane=value;
DidChangeValue("accountBalance");
}
}}
Main class:
public partial class AppDelegate:UIApplicationDelegate
{
Person person=new Person();
BankAccount bankAccount=new BankAccount();
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
bankAccount.AddObserver(person,new NSString("AccountBalance"),NSKeyValueObservingOptions.New |
NSKeyValueObservingOptions.Old|NSKeyValueObservingOptions.OldNew,IntPtr.Zero);
bankAccount.SetValueForKey(new NSNumber(100),new NSString("AccountBalance"));
bankAccount.setAccountBalance(1000);
}
}
Thanks for reading and sorry for this long post.
Does anyone have any help?
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Accessibility-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden