Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Programmatic bindings not working ?

crashmonkeycrashmonkey Posts: 216Registered Users @ @
edited July 2013 in Mac OS X Development
I need to create bindings during runtime. There will be model objects and view objects being created and destroyed at random that will need to be bound, so I cannot use interface builder to do it.

I have tried simple tests with the method bind:toObject:withKeyPath:options: but it does not seem to work as advertised. I have read examples in books, tutorials, and seen simple video demonstrations, but when I try to replicate them nothing happens.

Here is a simple example with code:
A view with a slider and a textField.
A controller object with an int property.
Bind the slider and textfield to the int property to change and reflect its value.
The view, slider, and textfield are created in IB and linked to the controller for simplicity.
#import <Cocoa/Cocoa.h>

@interface AppController : NSObject {

    int num;
    IBOutlet NSTextField *field;
    IBOutlet NSSlider *slider;

}
@property(readwrite, assign) NSSlider *slider;
@property(readwrite, assign) NSTextField *field;
@property(readwrite, assign) int num;
@end
//  AppController.m
#import "AppController.h"

@implementation AppController
@synthesize num, slider, field;

- (id)init
{
	[super init];
	[self setValue:[NSNumber numberWithInt:5]
			forKey:@"num"];

    [field   bind:@"value" toObject:self withKeyPath:@"num" options:nil];
    [slider bind:@"value" toObject:self withKeyPath:@"num" options:nil];

	return self;
}
@end
When I run it nothing happens. The controls remain in their default IB state.
If I bind the controls in IB and comment out the code, then it will work.

What is missing? I have seen examples simpler than this just binding a button label to a string and it works, but not when I try to replicate it.
Post edited by Rhade on
Learning IOS development one crash at a time. <img src="http://www.iphonedevsdk.com/forum/images/smilies/confused.gif"; border="0" alt="" title="Confused" class="inlineimg" />

Replies

  • crashmonkeycrashmonkey Posts: 216Registered Users @ @
    Gaaah!! Stupid me. I just realized the binding code has to be outside the controller -init .
    The controller is part of the nib so apparently the rest of the nib contents (slider, textfield) have not finished
    being created and intialized so there is nothing to bind to.
    I placed the binding code into a controller method and linked that to a button, so that when I pressed the button and invoked the method, then the binding works.

    Sorry to waste your time reading this, but for anyone as stupid as me, maybe this will help.
    Learning IOS development one crash at a time. <img src="http://www.iphonedevsdk.com/forum/images/smilies/confused.gif"; border="0" alt="" title="Confused" class="inlineimg" />
  • khunterkhunter Posts: 44New Users @
    For your information, there is an "awakeFromNib" method which is called on objects that are loaded from NIB files, after all of the stuff in the NIB is loaded and "hooked up." This is the perfect place to do things that have to be deferred until after all the NIB loading is done, but should be done before the object is used.
  • crashmonkeycrashmonkey Posts: 216Registered Users @ @
    Thats nice to know, but as stated at the beginning of my post, I will not be creating any nib files. The views and bindings will be created programmatically. In this particular example there is a nib just for testing, but I was not aware that -awakeFromNib was available to all objects. I thought that method was strictly a viewController method.
    Now I see that it is a category added to NSObject.
    Learning IOS development one crash at a time. <img src="http://www.iphonedevsdk.com/forum/images/smilies/confused.gif"; border="0" alt="" title="Confused" class="inlineimg" />
Sign In or Register to comment.