RE: Creating controls dynamically
RE: Creating controls dynamically
- Subject: RE: Creating controls dynamically
- From: "Rohit Bhatia" <email@hidden>
- Date: Mon, 28 Mar 2005 16:32:57 +0530
- Thread-topic: Creating controls dynamically
Hi,
Thanks for the useful suggestions. I have tried to implement them in my application and I am facing some issues. I created a subclass of NSView - MyView. I can create NSImage view controls programmatically by writing an implementation for initWithFrame. What I want is, that I should be able to create NSImageView type controls on some action, lets say, click of a Button.
Kindly have a look at the very simple code snippet below.
I want to be able to create controls when the function myAction() is called.
How can I change the contents of myImageView on a certain action (lets say change the image inside the view).
I feel I am missing something very basic in the way I am trying to implement this. Kindly suggest the correct method.
--CODE--
/* MyView.h*/
#import <Cocoa/Cocoa.h>
@interface MyView : NSView
{
IBOutlet id myOutlet;
NSImageView *myImageView;
NSImage *myImage;
}
- (IBAction)myAction:(id)sender;
- (id)initWithFrame:(NSRect) frameRect;
@end
#import "MyView.h"
@implementation MyView
- (IBAction)myAction:(id)sender
{
NSString *filename = [[NSString alloc] initWithString:@"/Users/MyHome/Desktop/RBIMG.jpg"];
myImage = [[NSImage alloc] initWithContentsOfFile:filename];
NSRect boxRect;
boxRect.origin.x = 10;
boxRect.origin.y = 10;
boxRect.size.height = 700;
boxRect.size.width = 700;
[self setNeedsDisplay:YES]; //Just trying...
[super setNeedsDisplay:YES]; //Just trying...
}
- (id)initWithFrame:(NSRect) frameRect
{
NSRect boxRect;
boxRect.origin.x = 10;
boxRect.origin.y = 10;
boxRect.size.height = 700;
boxRect.size.width = 700;
myImageView = [[NSImageView alloc] initWithFrame:boxRect];
if (self = [super initWithFrame:frameRect])
{
[self addSubview:myImageView];
}
NSString *filename = [[NSString alloc] initWithString:@"/Users/MyHome/Desktop/SomeOtherImage.jpg"];
myImage = [[NSImage alloc] initWithContentsOfFile:filename];
[myImageView setImage:myImage];
return self;
}
Regards,
Rohit
-----Original Message-----
From: Nicolas Zinovieff [mailto:email@hidden]
Sent: Tuesday, March 22, 2005 6:38 PM
To: Rohit Bhatia
Subject: Re: Creating controls dynamically
On 22 Mar 2005, at 13:46, Rohit Bhatia wrote:
> I am working on a window based application, and I am in a
> situation where I need to generate Picture Boxes (or NSImageView) at
> run time based on some specifications. For eg. I need to generate 5
> boxes at certain set of coordinates and of certain size, that can hold
> different JPEG images. A basic guidance for how to go about it would
> be really helpful. Kindly let me know how to add controls dynamically
> on a view.
Hi,
you could probably create your views offscreen, and add them as subviews of your main "always present" view.
That way, you keep the benefits of the IB stuff for interface, and don't have to worry much about risizing and stuff when you hook up your new views.
http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/ObjC_classic/Classes/NSView.html#//apple_ref/doc/uid/
20000014/BBCCCEII
--
Nicolas
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden