Rudiger
Rudiger

Reputation: 6769

Key Value Bindings iPhone

I have a bunch of NSDictionaries thats stored in an NSArray. The dictionaries store coordinates for buttons, their name, and their relating object to load when pressed (UIView).

When the button is pressed I can retrieve which button it is in string format, for example "settings" and I would like to then run the object -(void)settings{}.

Now I know how I would do it with if else statements but I would like to avoid them because they are messy and thought Key Value Bindings would be the way but I don't believe they exist on the iPhone.

Are they available on iPhone? Everything I read says no even in iOS4. If they are how would I implement this. If not is there a better way to do this other than if else statements? Could I store a reference to the UIView to push in the NSArray instead of a string and if so would this affect performance?

Cheers.

Upvotes: 0

Views: 1820

Answers (2)

Akash Kava
Akash Kava

Reputation: 39916

Key Value Binding does existing in iOS, but you have to do it manually. You can create a class derived from "Button" and you can create a property of NSDictionary which you can assign and at time of assigning, you can start observing changes in dictionary instance, and in the delegate you can read value and assign to self.

Ok here is my button.h file

@interface MyCustomButton : NSButton

NSDictionary* store;

@end

-(NSDictionary*) store;
-(void) setStore: (NSDictionary*) v;

and here is my button.m file

@implementation MyCustomButton


-(void) dealloc{
   [self setStore: nil];
   [super dealloc];
}

-(void) loadValues:{
   // fill your loading values here
   // this is not correct you may need
   // to see help to get correct names of
   // function
   [self setText: [store stringForKey:@"buttonLabel"]];
}

-(void) setStore: (NSDictionary*) v{
   if(store!=nil){
      [store removeObserver: self forKeyPath:@"buttonLabel"];
      [store removeObserver: self forKeyPath:@"buttonX"];
      [store removeObserver: self forKeyPath:@"buttonY"];
      [store release];
   }
   if(v==nil)
      return;
   store = [v retain];
   [store addObserver: self forKeyPath:@"buttonLabel" options:0 context:nil];
   [store addObserver: self forKeyPath:@"buttonX" options:0 context:nil];
   [store addObserver: self forKeyPath:@"buttonY" options:0 context:nil];

   [self loadValues];
}

-(NSDictionary*) store{
   return [store autorelease];
}

-(void) observeValueForKeyPath: (NSString*) keyPath 
   ofObject:(id)object 
   change:(NSDictionary*)change 
   context:(void*)context{
   // here you can identify which keyPath 
   // changed and set values for yourself
   [self loadValues];
}
@end

and probably in the beginning of code or whereever you have instance of MyButton you need to do following...

MyButton* button = [[[MyButton alloc] init] autorelease];

// add button to view...

[button setStore: myDictionary];
// from this point onwards button will automatically
// change its properties whenever dictionary
// is modified

Upvotes: 2

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

If you want to do runtime reference of classes and methods you need to have a look at Objective-C Runtime Reference. This will let you by using strings create classes and send messages to them.

Upvotes: 2

Related Questions