views:

181

answers:

2

Here's a simplification:

I have an application with several buttons. If it is the first time the application is launching, I want to do some special things. In my AppController class, which is a delegate of NSApp, I use the delegate method -applicationDidFinishLaunching: to perform the test. After I've detected that it is the first time, I first want to access some IBOutlets. Then, I'd like to be able to get mouse events for each button, so that I can do other things.

I can't figure out want to do with the classes. I'd like to make a new class (FirstLaunch) for the first launch, but I'm not sure what to call from AppDelegate. Also, to get mouse events, shouldn't I be a sublass of the buttons, and considering that I have multiple buttons, I'm confused. I could probably tackle these issues one-by-one, but taken all together, they're confusing me.

Broken down, I need to access & manipulate IBOutlets I have set in IB, determine when buttons are clicked (and which button was clicked). I'd like to be able to do this from another class so as to not clutter up the AppDelegate.

Thanks for the help!

To be more clear, what I'm actually trying to do is to use Matt Gemmel's MAAttachedWindow to put up a help bubble by a button. When the button is clicked clicked, the bubble disappears and another one is put somewhere else. The bubbles will be attached to controls in the main window.

+2  A: 

I'm guessing you want to show some additional user interface on the first launch? If it's a separate window, I'd advise creating a subclass of NSWindowController. Add a new NIB file for the first-run user interface to your project and change the class of the File's Owner object to FirstLaunch. Control-drag a wire from the File's Owner delegate onto the window to connect it with the window outlet.

You create IBOutlets by adding an instance variable to the class. If your app will only run on Leopard or higher, it's better to declare your outlets like this:

@interface FirstLaunch : NSWindowController {
    NSTextField *myTextField;
}

@property (nonatomic, retain) IBOutlet NSTextField *myTextField;

@end

In Interface Builder, you'll control-drag a wire from the File's Owner onto the control to associate it with that outlet. Make sure that you release your reference to each IBOutlet in your class's dealloc method (if you're not using garbage collection) or else your app will leak memory.

Buttons send action messages when they're clicked, so you'll need to provide an action method for the button to call. You do that by declaring a method with a signature like this:

- (IBAction)myButtonClicked:(id)sender;

In Interface Builder, you'll control-drag a wire from the button onto your window controller and choose the myButtonClicked: method.

To make all this work, you'll need to create an instance of the window controller and tell it to load the NIB file at runtime. So, in your AppDelegate class, when you've determined that this is the first launch, you'll do this:

FirstLaunch *firstLaunchController = [[FirstLaunch alloc] initWithWindowNibName:@"nameOfNibFile"];
[firstLaunchController show:self];

You'll probably want to keep the reference to the window controller in an instance variable instead of a local variable like I've done here. And, depending on your application, it may make more sense to show this as a sheet. But once you've made it this far, you'll be able to figure out how to do that on your own.

Alex
that does help for something else I want to do, but I've clarified my purpose further by editing my questionthanks, though!
Walker Argendeli
A: 

Then, I'd like to be able to get mouse events for each button, so that I can do other things.

Don't worry about the mouse. There may not even be a mouse (think of the ever-popular tablet-Mac rumor).

I'd like to make a new class (FirstLaunch) for the first launch, but I'm not sure what to call from AppDelegate.

You make your own methods here. You'll probably make it a singleton*; then, you'll implement a method named something like runFirstLaunchPanel:, which will be an action method (more on those in a moment):

- (IBAction) runFirstLaunchPanel:(id)sender;

Instantiate the object in the nib, then, from your app delegate, call the action method with nil as the sender.

The reason to put the object in your nib and make the method an action method is that this makes it easy to hook up a menu item to it, so that the user can re-run the first-launch panel at a later time. (For example, if it's a Starting Points window, you might connect the New menu item to this action instead of the default one.)

*Yes, I've seen the articles about singletons, and I agree with them. In a case like this, it's OK.

Also, to get mouse events,

This is the wrong way of thinking about it. What you need to do is set your button up to send a message to your controller to make the controller (probably AppDelegate) do something. The message you want the button to send is an action message.

Implement an action method in the object that owns the nib containing the window with the buttons. Declare this method in the class's header, then connect the button to it in IB by right-clicking on your controller and dragging from the correct action method's circle to the button.

This is called the target-action paradigm, and it insulates controller responsibilities (doing things) from the views that ordered them. Because each action method does only one thing, you can have a button, a menu item, and even another controller (your app delegate, above) send the same action message, and the receiving controller won't have to care which control is sending the action, because it already knows what it has to do.

shouldn't I be a sublass of the buttons,

No. You very rarely create subclasses of anything other than NSObject (or, for model objects in Core Data, NSManagedObject) in Cocoa.

Note that I said “rarely”, not “never”. You will have to make the occasional subclass, especially if you want to create custom or customized views and cells (and, maybe, customized windows). However, subclassing is not necessary in Cocoa to the degree that (I hear) it is in some other frameworks on other platforms.

and considering that I have multiple buttons, I'm confused.

The target-action paradigm means you don't have to create one button subclass per button. One controller class implements all the actions, and the stock buttons, because you've hooked them up in IB, simply tell the controller “do this”.

Broken down, I need to access & manipulate IBOutlets I have set in IB,

Probably not. At least, not as much as you think you do.

determine when buttons are clicked (and which button was clicked).

Nope. The buttons will worry about being clicked; you just worry about setting them up to send, and then responding to, their action messages.

Peter Hosey