Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Creating new instances of something

Creating new instances of something
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Aug 13, 2004, 05:55 PM
 
Hi guys,

I have a custom NSView, and in it I want to display various different controls (NSButton's, NSPopUpButton's etc.)

I can do this quite easily. So now I want to make it, so when you click one of the NSButton's, a NSPopUpButton menu shows up. So I make this code:

Code:
- (void)addButtonOnRightWithImage:(NSImage *)image withAction:(SEL)action withMenu:(NSMenu *)menu { NSImage *seperatorImage; NSImageView *imageView; addedButtonSize.width = addedButtonSize.width + 26; button = [[NSButton alloc] initWithFrame:NSMakeRect(NSWidth([self frame]) - 10 - addedButtonSize.width,0,17,17)]; [[button cell] setButtonType:NSToggleButton]; [[button cell] setBezelStyle:NSRegularSquareBezelStyle]; [button setBordered:NO]; [button setAutoresizingMask:NSViewMinXMargin]; [button setTarget:self]; [button setImage:image]; if (action) [button setAction:action]; else [button setAction:@selector(popUpMenu:)]; [self addSubview:button]; seperatorImage = [[MRInterfaceFactory sharedFactory] tableHeaderNormalSeparator]; imageView = [[NSImageView alloc] initWithFrame:NSMakeRect(NSWidth([self frame]) - 15 - addedButtonSize.width,0,1,17)]; [imageView setImage:seperatorImage]; [imageView setAutoresizingMask:NSViewMinXMargin]; [self addSubview:imageView]; if (menu) { menuPopUpButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(NSWidth([self frame]) - 10 - addedButtonSize.width,0,17,0)]; [[menuPopUpButton cell] setMenu:menu]; [[menuPopUpButton cell] setControlSize:NSSmallControlSize]; [menuPopUpButton setFont:[NSFont systemFontOfSize:11.0]]; [menuPopUpButton setBordered:NO]; [self addSubview:menuPopUpButton]; } } - (void)popUpMenu:(id)sender { [menuPopUpButton performClick:nil]; }
Note that 'menuPopUpButton' is defined in the header. I build and click a button, menu seems to appear. When I click another button configured nearly the same, the menu shows up, but it shows up in the same position as when I clicked the other button. I know why this is (because I defined it in the header, so its basically adding the same object), the reason I need to put it in the header is due to the performClick method, I can't use sender because the NSButton is sending the method.

So, I'm a bit stuck as to what I should try next.

Any help appreciated,
Oliver
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Aug 15, 2004, 05:37 PM
 
It's a little unclear what you're trying to achieve here. Any reason you don't use two separate popup menus? Any reason you don't position them in the NIB, instead of programatically? (You could always have them hidden).

Why can't you use the action's "sender"? You can use it to tell which button send the message then (so long as you define the buttons as IBOutlets and connect them in your NIB).
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Aug 16, 2004, 04:37 AM
 
Originally posted by Brass:
It's a little unclear what you're trying to achieve here. Any reason you don't use two separate popup menus? Any reason you don't position them in the NIB, instead of programatically? (You could always have them hidden).

Why can't you use the action's "sender"? You can use it to tell which button send the message then (so long as you define the buttons as IBOutlets and connect them in your NIB).
The reason I am doing this programatically is so that I can easily add buttons to this Aqua Bar thing, its just convenience really. And it also automatically moves it the appropriate distance to the left of the other button. And I can't use sender, because the object that is actually sending the method is the NSButton, and not the NSPopUpButton. I would use NSPopUpButton, but you don't get the grey layover the button when you click it.

Thanks,
Oliver
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 16, 2004, 11:26 AM
 
"Grey layover the button"?

And I don't understand what you mean "defined it in the header." You mean you declared it in your header file with global scope or what? It sounds like you understand what is wrong with this situation, though. Can you explain?

And if, as you say, you just want the menu, why don't you just pop up the menu yourself? It sounds like maybe you're creating some sort of temporary NSPopUpButton just to pop up a menu or something?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Aug 16, 2004, 12:07 PM
 
Originally posted by Chuckit:
"Grey layover the button"?

And I don't understand what you mean "defined it in the header." You mean you declared it in your header file with global scope or what? It sounds like you understand what is wrong with this situation, though. Can you explain?

And if, as you say, you just want the menu, why don't you just pop up the menu yourself? It sounds like maybe you're creating some sort of temporary NSPopUpButton just to pop up a menu or something?
Firstly, I mean I put something like 'NSPopUpButton *popUpButton;', not sure if the correct term is defined. But its only in the header file of the NSView subclass.

My first choice was to pop up the menu myself, but I could never get it to work at all, do you know any working sample code?

Thanks,
Oliver
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Aug 16, 2004, 06:04 PM
 
I still don't understand why you can't use the "sender" parameter. The "sender" will always be the button that was clicked. You can then make code that decides what to do based on the sender.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 17, 2004, 01:44 AM
 
Originally posted by iOliverC:
My first choice was to pop up the menu myself, but I could never get it to work at all, do you know any working sample code?
I think NSMenu has some methods for popping up a menu. Otherwise, there's the Carbon Menu Manager.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Aug 17, 2004, 04:00 AM
 
Originally posted by Brass:
I still don't understand why you can't use the "sender" parameter. The "sender" will always be the button that was clicked. You can then make code that decides what to do based on the sender.
I tried sender and got a signal 10 crash, basically, what I'm doing (I think) is attaching an action to the NSButton below the NSPopUpButton, and since the NSPopUpButton is 0 in height, its invisible, so its un-clickable. So when you click the NSButton (which is the sender I believe), it performs the action popUpMenu:, which then does performClick:nil, which is a NSPopUpButton method. So if I used sender, it would perform performClick: on the NSButton, which is not what I want.

Thanks,
Oliver
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Aug 17, 2004, 04:01 AM
 
Originally posted by Chuckit:
I think NSMenu has some methods for popping up a menu. Otherwise, there's the Carbon Menu Manager.
Seems like this is the method I'll have to go with.

Thanks everyone for the help,
Oliver
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Aug 17, 2004, 06:23 PM
 
Originally posted by iOliverC:
I tried sender and got a signal 10 crash, basically, what I'm doing (I think) is attaching an action to the NSButton below the NSPopUpButton, and since the NSPopUpButton is 0 in height, its invisible, so its un-clickable. So when you click the NSButton (which is the sender I believe), it performs the action popUpMenu:, which then does performClick:nil, which is a NSPopUpButton method. So if I used sender, it would perform performClick: on the NSButton, which is not what I want.

Thanks,
Oliver
I think you need to post some code so we can see what you are talking about here. If you send performClick:nil, the the sender is nil, and some things you do with that nil will cause a crash.

But if you use performClick:self (or performClick:<object> where object is something meaningful) then surely you can do an "if ( sender == <object1> ) ... else if ( sender == <object2> ) ...".
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Aug 18, 2004, 02:48 PM
 
I've fixed it all, added a method for generating a menu, so no need for the NSPopUpButton now.

Thanks again everyone
Oliver
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:08 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2