NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,100,100)];
[myButton setImage:myImage];
Use setTarget:/setAction: to set the target object (should be in the controller, not the view layer so not directly a NSWindow, rather something in your controller layer that will update the model layer which will then cause your view to update via the controller layer)
the action (the method that will be executed on the target object)
[myButton setAction:[self changeWindow:self]];
[myButton setTarget:self]; // Sets the target of the action to this object
[myButton setAction:@selector(handleButton:)]; // Sets the action
....
-(void) handleButton:(id) sender
{
// Handle button click here
}
[myButton setAlternateImage:@"FSInfo.tif"];
Thanks Robbie for showing how to set the target and action, but I decided to go back and look at using an icon for the button but when my code is:
Code:[myButton setAlternateImage:@"FSInfo.tif"];
all it does is create a button without anything in it, am I missing anything out? FSInfo.tif has been added to the Resources folder of the project in Xcode.
Thanks,
Stephen
- (void)setAlternateImage:(NSImage *)image
- (void)setAlternateImage:(NSString *)image
NSString *imageName = [[NSBundle mainBundle] pathForResource:@"FSInfo.tif" ofType:@"TIF"];
NSImage *tempImage = [[NSImage alloc] initWithContentsOfFile:imageName];
[myButton setImage:tempImage];
[myButton setImagePosition:NSImageOnly];