Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 6, 2009, 01:23 PM   #1
SRossi
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
Create button programmatically

Hi all,

Me yet again with another question. What I'm trying to do now is create a button programmatically with a custom picture as the front of the button.

Any guides or examples or places to look will be appreciated.

Thanks again,

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old Jul 6, 2009, 01:45 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
NSButton

Code:
NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,100,100)];
[myButton setImage:myImage];
Should create a 100x100 button (at 0,0 initially) with image set to myImage (a NSImage you have to deal with creating)...

Edit to add: not I just typed this straight into the reply: it could be buggy. Also this won't actually show a button anywhere: you need to add it as a subview to a view in a window.
robbieduncan is offline   0 Reply With Quote
Old Jul 6, 2009, 02:23 PM   #3
SRossi
Thread Starter
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
Right thanks got a button set up but now how will I set up the button to call another window when pressed?

This window has already be connected to another outlet. Can I call the original method to call the other window if so how would this be done?

Thanks,

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old Jul 6, 2009, 03:35 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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) and the action (the method that will be executed on the target object). Basically this is the same as dragging in IB and choosing the method to execute.
robbieduncan is offline   0 Reply With Quote
Old Jul 6, 2009, 06:17 PM   #5
SRossi
Thread Starter
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
Quote:
Originally Posted by robbieduncan View Post
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)
Can you explain what you mean by this because im not understanding what you are meaning, like should it be the target of a window controller?

Quote:
Originally Posted by robbieduncan View Post
the action (the method that will be executed on the target object)
What I have done for this is :

Code:
[myButton setAction:[self changeWindow:self]];
It does not give an error but says "Invalid use of void expression", this peice of code is in the awakeFromNib function. changeWindow is the function used to change the windows.

Thanks for your help

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old Jul 7, 2009, 02:53 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
The target should be a valid instance in the controller layer. The NSWindowController subclass that owns this window is a decent idea.

The action is not an invoked method (which is what you have) rather a description of the method. Something like:

Code:
[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
}
Again this is just typed straight into MR: it may have typos.
robbieduncan is offline   0 Reply With Quote
Old Jul 7, 2009, 09:39 AM   #7
SRossi
Thread Starter
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
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
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old Jul 7, 2009, 09:42 AM   #8
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by SRossi View Post
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
You need to learn to read the documentation carefully. The signature of that method is:

Code:
- (void)setAlternateImage:(NSImage *)image
You are trying to call

Code:
- (void)setAlternateImage:(NSString *)image
You need to actually load the image...
robbieduncan is offline   0 Reply With Quote
Old Jul 7, 2009, 10:21 AM   #9
SRossi
Thread Starter
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
Right have changed my code again, taking into account what you have said so it now looks like:

Code:
NSString *imageName = [[NSBundle mainBundle] pathForResource:@"FSInfo.tif" ofType:@"TIF"];
	NSImage *tempImage = [[NSImage alloc] initWithContentsOfFile:imageName];
	[myButton setImage:tempImage];
	[myButton setImagePosition:NSImageOnly];
But now it just shows a blank button again, without the NSImageOnly it included a string of "button" though.

I'm thinking im missing something but looking through the documentation it should work?

Thanks again,

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote
Old Jul 7, 2009, 10:25 AM   #10
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Just guessing here but you are not a professional programmer? Just learning? Because you should check that the objects being returned are actually what you expect. And once again read the documentation!

I would expect that imageName is nil as you are asking for resource FSInfo.tif of type TIF which, as per the example in the documentation you should not be doing. Also, as per the documentation, you should check for the returns to most of these calls being nil.

Please, read the documentation, do the sensible sanity checks (nil objects, compiler warnings etc) before asking questions!
robbieduncan is offline   0 Reply With Quote
Old Jul 7, 2009, 10:40 AM   #11
SRossi
Thread Starter
macrumors regular
 
Join Date: May 2009
Location: Glasgow, Scotland
Ah ha cracked it, thank you Robbie for all your help so far. And sorry if I have been a nuisance to you.

Setting the image turned out harder than I would have ever believed but its made me understand bundles better.

Thanks Robbie again.

Stephen
__________________
macMini, 2.0 GHz Core 2 Duo, 1 GB RAM, 120GB hd;
Dell S2209W 21.5" monitor;
iPhone 4GS 16GB;
30 GB iPod video;
SRossi is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: How to create a UIImage RGB gradient with a portion of black programmatically troop231 iPhone/iPad Programming 5 Jan 13, 2014 09:25 PM
Programmatically sending messages Mvkoe iPhone/iPad Programming 1 Jan 18, 2013 06:13 AM
Is there a way to programmatically change the language being used? NukemHill iPhone/iPad Programming 0 Dec 12, 2012 03:22 PM
iPhone 4 Sleep/wake button, vibrate button, and volume button not responsive McEggrolls iPhone 6 Sep 20, 2012 09:16 AM
How to create a Modal View Programmatically (and dismiss it) roeik iPhone/iPad Programming 10 Aug 15, 2012 02:36 PM

Forum Jump

All times are GMT -5. The time now is 09:39 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC