Reputation: 15
After creating many NSButton by programmatically, how can I get it back and delete it on view?
Here's method to create NSButton
- (void)createButton:(NSString *)buttonName
title:(NSString *)buttonTitle
x:(int)xValue
y:(int)yValue
width:(int)widthValue
height:(int)heightValue
filePath:(NSString *)filePathValue
fileTypeCode:(enum FILE_TYPE)fileTypeValue
duration:(int)durationValue
indexOnTimeline:(int)index
{
NSButton *btn = [[NSButton alloc] initWithFrame:
NSMakeRect(xValue,yValue,widthValue,heightValue)];
[[_window contentView] addSubview: btn];
NSString *moreDesc = [NSString stringWithFormat:@"%@:%i:%i:%i", filePathValue, fileTypeValue, durationValue, index];
[btn setAlternateTitle:moreDesc];
[btn setTitle: buttonTitle];
[btn setTarget:self];
[btn setIdentifier:buttonName];
[btn setAction:@selector(renderMe:)];
[btn setButtonType:NSMomentaryLight];
[btn setBezelStyle:NSTexturedRoundedBezelStyle];
}
Upvotes: 0
Views: 1260
Reputation: 1357
You can do a couple things
1.) Change method signature to return NSButton. In the method that calls create button, you can add all the buttons to an NSMutableArray. In your header file, define a new strong property NSMutableArray* buttonArray.
- (void)callingMethod {
self.buttonArray = [NSMutableArray array];
[self.buttonArray addObject:[self createButton:***]]
}
- (NSButton*)createButton:(NSString *)buttonName
title:(NSString *)buttonTitle
x:(int)xValue
y:(int)yValue
width:(int)widthValue
height:(int)heightValue
filePath:(NSString *)filePathValue
fileTypeCode:(enum FILE_TYPE)fileTypeValue
duration:(int)durationValue
indexOnTimeline:(int)index
{
... Your code ....
return btn;
}
2.) Alternatively, you can access all the subviews that your view has by calling:
NSArray* subViews = [[_window contentView] subviews];
foreach(NSView* view in subViews) {
if([view isMemberOfClass:[NSButton class]]) {
NSButton* button = (NSButton*) view;
// Figure out if the button is the one you want and do something to it
}
}
Upvotes: 1
Reputation: 3
You need to change the method signature returning the instance of the button. You also need to remove the part where you are adding to the contextView the button created.
For a non ARC world you also need to autorelease the returned object.
Andrea
Upvotes: 0
Reputation: 15
I solved this, very simple, I use an NSMutableArray to store all buttons I created, then you can access and do anything on any button in this array.
Upvotes: 0