-1

In order to access -mouseUp and -mouseDown events in a button I have subclassed NSButton and I also want to store a few extra variables within the object:

@interface MoveButton : NSButton{

    unsigned char cmd[3];
    unsigned char stop[3];
    int r;
    struct libusb_device_handle *devh;

}

-(void)mouseDown:(NSEvent *)theEvent;
-(void)mouseUp:(NSEvent *)theEvent;
-(void) setcommand:(char*)first:(char*)second:(char*)third (libusb_device_handle*)device_handle;
-(void) setLight: (char*) light;

@end

Within my window controller I have a number of instances of the subclass each connected to an element in Interface Builder through an IBOutlet.

@interface MainWindowController(){

    IBOutlet MoveButton * base_cw;
    IBOutlet MoveButton * base_ccw;

@end

Within the initWithWindow function each of the buttons are initialised:

 base_cw = [[MoveButton alloc] init];
 base_ccw = [[MoveButton alloc] init];
 [base_cw setcommand:"00" :"00" :"00":devh];
 [base_ccw setcommand:"00" :"00" :"00":devh];

In Interface builder I have changed the class property of each button to MoveButton and connected it to the correct IBOutlet. However when a button is actually clicked on the UI errors are thrown up because the instance variables the it is trying to use are empty. Is it that I haven't connected the in-code object properly with the UI element or am I just going about it the wrong way entirely?

  • Have you initialised the class itself? and why you don't use ARC, no need of ivar, simply create property. and use _base_cw=allocinit etc – Anoop Vaidya Dec 29 '12 at 15:18

2 Answers2

1

Having the lines

base_cw = [[MoveButton alloc] init];
base_ccw = [[MoveButton alloc] init];

in your implementation of -initWithWindow: is a mistake. These values will be overwritten by the time -windowDidLoad is called, assuming that you have hooked up the controls (your MoveButtons) to MainWindowController (File's Owner) in Interface Builder. If you have not hooked up the controls (instances of MoveButton) in Interface Builder, you'll have even more problems.

After double-checking that you have hooked up MainWindowController's ivars base_cw and base_ccw to the appropriate MoveButtons in Interface Builder, you just need to add the lines configuring the buttons to -windowDidLoad:

- (void)windowDidLoad
{
    //...
    [base_cw setcommand:"00" :"00" :"00":devh];
    [base_ccw setcommand:"00" :"00" :"00":devh];
}

Your selector setcommand:::: doesn't conform to Objective-C conventions. A better name would be

- (void)setFirstCommand:(char *)firstCommand 
        secondCommand:(char *)secondCommand 
        thirdCommand:(char *)thirdCommand 
        deviceHandle:(libusb_device_handle *)deviceHandle;

Notice the use of a label for each argument (secondCommand for secondCommand and so forth) and the use of camel case (setFirstCommand:... rather than `setfirstcommand:... and so on).

Nate Chandler
  • 4,533
  • 1
  • 23
  • 32
  • The WindowController is instantiated within the NIB itself rather than being created programatically, so I can't get the windowDidLoad function to call, but I have moved all the initialisation of the MoveButton objects for the time being so that it definitely runs and from that point the MoveButton object contain the necessary data. I have connected all the IBOutlets to the elements on the window but I am still having the same problem of when the buttons are clicked all the ivars are empty. – Sam Payne Dec 30 '12 at 17:10
  • Since your window controller is in your `xib`, use `-awakeFromNib` rather than `-windowDidLoad`. If the outlets are properly set up in Interface Builder, your button instance variables will be non-`nil` at this point. – Nate Chandler Dec 30 '12 at 18:11
  • In general, though, it is not best practice to have your window controllers in your `xib`s. – Nate Chandler Dec 30 '12 at 18:12
0

Fixed! By using the declaration

IBOutlet MoveButton * base_cw;

It wasnt truly linking the in-code object to the UI object.

Replacing it with:

IBOutlet id base_cw

The link was properly established and messages to change the ivars were being sent to the correct object.

Many thank for the help given.