Tuesday, September 15, 2009

How do you rotate an NSButton, NSTextField, or NSView?

How do you rotate an NSButton, NSTextField, or NSView?

First, you can do it and it is fairly easy to do! Here is what it will look like!



There are two NSView methods available to do this.

In OS 10.0 you have:

- (void)setFrameRotation:(CGFloat)angle

And in OS 10.5 you have:

- (void)setFrameCenterRotation:(CGFloat)angle

For some the above information will be sufficient. For those desiring more information please continue to read this posting.

Create a new "Cocoa Application". From the File menu choose "New Project" and then select "Cocoa Application".
















Name and save the project whatever you want. I named my project "RotatedControl".

In XCode add to the "Classes" folder a "New File". Choose "Objective-C class".

I named my class "Controller"















Add your IBOutlets to your new class. Here is my code:

//
// Controller.h
// RotatedControl
//
// Created by Geoffrey_Slinker on 9/13/09.
// Copyright 2009 Area 51. All rights reserved.
//

#import


@interface Controller : NSObject {
IBOutlet NSTextField *textField;
IBOutlet NSButton *button;
}

@property (retain) NSTextField *textField;
@property (retain) NSButton *button;

- (void) awakeFromNib;

@end

//
// Controller.m
// RotatedControl
//
// Created by Geoffrey_Slinker on 9/13/09.
// Copyright 2009 Area 51. All rights reserved.
//

#import "Controller.h"


@implementation Controller

@synthesize textField;
@synthesize button;

- (void) awakeFromNib
{
//[textField setFrameRotation:45.0];
//[button setFrameRotation:30.0];

[textField setFrameCenterRotation:45.0];
[button setFrameCenterRotation:-45.0];


}

@end


Now open the NIB file. In XCode double click "MainMenu.xib" found in the folder "NIB Files". This should launch Interface Builder (IB).

Add an "Object Controller" and set its class to be the class type you created above. In my case I set it to "Controller". See "Where is the Classes Tab" for detailed information.

Still in IB add a Button and a Text Field to your Window. Just select them from the Library of Objects and drag them onto the Window.



Now "hook up" the IBOutlets of the Controller to the controls on the window.


You can see I have "wired" or "connected" the button outlet to the button on the window. The same was done for the text field.

Also wire the the "Referencing Outlets" delegate to the "Application". The "Application is above the "Controller" in the MainMenu.xib view.



If I haven't missed any steps then from XCode just build and run the application.



Monday, September 14, 2009

Where is the Classes Tab in Interface Builder? It has been removed.

Where is the Classes Tab in Interface Builder? It has been removed.

Interface Builder 3.1.2 does not have it. I do not know which version was the first to "not have it" but I know my version doesn't.

This can cause some confusion with tutorials, texts, books, etc., that refer to the Classes Tab.

Many examples refer you to a screen shot of IB's UI like this one:


Well, stop looking for it because it is gone.

Interface Builder 3 looks like this:


For instance, if you are creating a class that has some IBOutlet's then create that class in XCode, add the IBOutlet's that you desire. Launch Interface builder by doubling clicking your NIB file in XCode.

Select an Object from this panel:



Drag the "Object" to "Main***.xib" window, the window that has "File's Owner, First Responder, etc".

Make sure this new object is selected and from the Tools Menu open the Identity Inspector. There is a menu at the top labeled "Class". Click this and select the class that you created in XCode. The one I created I named "Controller", so I select "Controller" from the Class drop down menu.


I hope that helps clear up things for some.