Key-value observing

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Key-value observing

Haya
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?