Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RaulAmor

macrumors newbie
Original poster
Apr 25, 2010
6
0
How would I implement an NSView so that whenever text is typed into an NSTextField and an NSButton is pressed, the text will be drawn into the NSView?

I have an Custom View in my GUI in Interface Builder, how would I connect it from there?
 
If you have a custom view then you're going to have to have a subclass of NSView in your code and do the drawing in drawRect:. You're going to have to write code: not use IB.
 
Alright, so in my view class I should have a method to draw (for example) NSString *string to the view, and I call this method from drawRect.

Now do I create an instance from of that view class in my controller class, set "string" to whatever the NSTextField contains, then simply call setNeedsDisplay on that instance?
 
Ok, so the extra method for drawing is unnecessary.

Well I tried what I had described in my previous post and it didn't work. Is it because I'm working through an instance and not the actual object?

I can't figure out how to connect the NSTextField text to the view class's string variable any other way...
 
Well I tried what I had described in my previous post and it didn't work. Is it because I'm working through an instance and not the actual object?

I have no idea what this is supposed to mean. An instance is an object.

I can't figure out how to connect the NSTextField text to the view class's string variable any other way...

That's what your controller layer in a well designed MVC application is for.
 
I have no idea what this is supposed to mean. An instance is an object.

Sorry, I mistyped that. By creating an instance of the view class my Custom View is attached to, should I be able to modify the Custom View through that instance?
 
Sorry, I mistyped that. By creating an instance of the view class my Custom View is attached to, should I be able to modify the Custom View through that instance?

No. That makes no sense at all. Your custom view is an instance. You need to set the class of that to your custom class in Interface Builder. Do not create another instance of it and hope that altering one object will somehow effect another.
 
Ok, I had done that originally so I guess that's good news.

So how do I get my controller class to communicate with/modify the Custom View?
 
Ok, I had done that originally so I guess that's good news.

So how do I get my controller class to communicate with/modify the Custom View?
You may want to slow down. :)

Try working through this tutorial. It will really help you both in the short- and long-term.

http://cocoadevcentral.com/d/learn_cocoa_two/

If you're still having problems, or wish to investigate further, CocoaDevCentral has a lot of other great tutorials, and this book by Aaron Hillegass is pretty great:

http://www.amazon.com/Cocoa-Programming-Mac-OS-3rd/dp/0321503619/ref=sr_1_1?ie=UTF8&s=books&qid=1272245838&sr=8-1

Also, I want to finally note that I actually had to do something like this earlier today. Here's the code I used, I won't tell you what it does or what to change, I'll leave it as a lesson for the reader. Happy coding! :)

Code:
- (void)drawRect:(NSRect)dirtyRect {
	NSGradient *backgroundGradient = [[NSGradient alloc] initWithStartingColor:[NSColor grayColor] endingColor:[NSColor blackColor]];
	[backgroundGradient drawInRect:dirtyRect angle:90];

	NSMutableDictionary *drawStringAttributes = [[NSMutableDictionary alloc] init];
	[drawStringAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
	[drawStringAttributes setValue:[NSFont fontWithName:@"American Typewriter" size:24] forKey:NSFontAttributeName];
	NSShadow *stringShadow = [[NSShadow alloc] init];
	[stringShadow setShadowColor:[NSColor blackColor]];
	NSSize shadowSize;
	shadowSize.width = 2;
	shadowSize.height = -2;
	[stringShadow setShadowOffset:shadowSize];
	[stringShadow setShadowBlurRadius:6];
	[drawStringAttributes setValue:stringShadow forKey:NSShadowAttributeName];	
	[stringShadow release];
	
	NSString *MRString = @"Hello RaulAmor!";
	NSString *budgetString = [NSString stringWithFormat:@"%@", MRString];
	NSSize stringSize = [budgetString sizeWithAttributes:drawStringAttributes];
	NSPoint centerPoint;
	centerPoint.x = (dirtyRect.size.width / 2) - (stringSize.width / 2);
	centerPoint.y = dirtyRect.size.height / 2 - (stringSize.height / 2);
	[budgetString drawAtPoint:centerPoint withAttributes:drawStringAttributes];
	[drawStringAttributes release];
}
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.