开发者

XCode Cocoa questions (VB.NET to Cocoa)

Well, I've been programming for VB.NET for a while, and now I am interested in programming Mac applications instead.

I know Objective-C alright. But, of course, I need to understand Cocoa/Xcode's development environment. I understand the Interface Builder is the equivalent to the Form Designer in VB.NET and other common co开发者_如何转开发ncepts both environments seem to share.

Now, I will begin with something basic:

In VB.NET, I would use the Paint function to get a Graphics object and draw a red rectangle somewhere in my window. What is the equivalent in Cocoa?

Thank you. I know my question is quite specific (how to make a red rectangle in the window), but that is one of the few things I need to understand better since I can work from that point on.


The most straight-forward way to do something like that would be to subclass the standard view class and implement the -[drawRect:] method. These are handled slightly different between UIKit (iOS) and AppKit (Mac OS X). For example, to paint a red box the size of a view's visible bounds in iOS, you'd subclass UIView and then implement a drawRect: like so:

- (void)drawRect:(CGRect)rect
{
  [[UIColor redColor] setFill]; // set the fill color
  UIRectFill([self bounds]);    // fill a box (this view's visible bounds)
}

To do the same thing in Mac OS X, you'd subclass NSView and then implement a drawRect: like so:

- (void)drawRect:(NSRect)rect
{
  [[NSColor redColor] setFill]; // set the fill color
  NSRectFill([self bounds]);    // fill a box (this view's visible bounds)
}

For more information, you can take a look at the Cocoa Drawing Guide for working with Mac OS X and AppKit or the Drawing and Printing Guide for iOS for working with iOS and UIKit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜