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

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
I've been using various online resources lately to begin learning programming and I'm having an absolute blast. I'm mainly working on C/C++, but I was trying this Objective-C Cocoa tutorial as a "fun" project.

Unfortunately this tutorial is for an earlier version of Xcode and Xcode 3.1 has changed the way Interface builder handles classes. I can't figure it out to save my life. The tutorial states that 3.1 should automatically find classes. But I just can't find the outlet and action I need to make the button work. Any ideas? :confused:

Thanks!
 
Unfortunately, at the rate Apple makes changes; a lot of online tutorials no longer are accurate.

If you have Xcode and IB open at the same time, and add outlets or actions to the header in Xcode, then IB should see them. (The "old way", which is to drag the header file declaring the outlet/action from the Finder into the Nib window in IB should also still work, I assume).
 
Yeah, though at least this one has a disclaimer!

That could be my problem; I'll try adding the outlet and action into the header with IB open. When I did it first, I didn't have IB open. The drag and drop does not seem to work.

Thanks!
 
Didn't seem to help...I must be missing something very obvious. :eek:

Anyone else? :apple:
 
Do you have a sender argument on your IBAction method? IB won't discover methods without only a single argument of type id.

I haven't check if the tutorial goes over this, but to connect to controller and delegate objects, you need to add it to the MainMenu.xib window in IB. Drag the "Object" item from the Library window, then, with the new Object selected, click the Object Identity tab (the info icon) of the Inspector window, and select your class from the drop-down.

After looking over the tutorial, it is quite out of date. After writing your class in XCode, open up IB and then follow the above to create the instance, then jump to step 14, of the tutorial.
 
Perhaps it would help if everyone were on the same "page"? What tutorial are you referring to?

Edit: Sorry didn't see the link till I reread it.
 
Do you have a sender argument on your IBAction method? IB won't discover methods without only a single argument of type id.

I haven't check if the tutorial goes over this, but to connect to controller and delegate objects, you need to add it to the MainMenu.xib window in IB. Drag the "Object" item from the Library window, then, with the new Object selected, click the Object Identity tab (the info icon) of the Inspector window, and select your class from the drop-down.

After looking over the tutorial, it is quite out of date. After writing your class in XCode, open up IB and then follow the above to create the instance, then jump to step 14, of the tutorial.

Thanks for your reply! I know it's out of date; in fact is says so at the top.

I'm a n00b at programming in general, and even worse with Objective-C and Cocoa. So in order to avoid confusion:

This is from my header, AppController.h:

Code:
#import <Cocoa/Cocoa.h>

@interface AppController : NSObject 
{
	IBOutlet id textView
}
-(IBAction) clearText: sender;
@end

And from AppController.m:

Code:
#import "AppController.h"


@implementation AppController

-(IBAction) clearText: sender
{
	[textView setString: @" "];
}
@end

Just to be clear, I'm doing this tutorial purely for fun. I'd love to finish this project, though.
 
I figured it out! Your help in IB was exactly what I needed, as I think I had the code right. The app now works exactly as it should after compile. The tutorial doesn't cover it, but what steps might I need to take to allow the app to save and all that?
 
One method is to create an AppDelegate Class which conforms to NSApplicationDelegate:

@interface AppDelegate : NSObject <NSApplicationDelegate> {
}

Add an instance of the class to IB, just like with your controller class, and connect your class to the delegate outlet of the "File's Owner" item in IB.

You can then implement the applicationWillTerminate method into your class with whatever you would like to happen as the application quits. NSUserDefaults is the the best place to save preferences type information:

- (void) applicationWillTerminate:(NSNotification *)notification {
}

If you have IB controls, window sizes and positions you want to save. You can do so with no code right in IB. Select the item you want to save, select the Bindings tab in the inspector, select the property you want to save, check the box next to "Bind to: Shared User Defaults".

You can also use bindings to save most application data, though I'm not very familiar with the process. I understand that it's fairly simple though.
 
OK, a n00b question. Bear with me since this is probably a groaner. :eek:

Where do the AppDelegate class and the NSUserDefaults thing go? So sorry if that's perfectly obvious!
 
OK, a n00b question. Bear with me since this is probably a groaner. :eek:

Where do the AppDelegate class and the NSUserDefaults thing go? So sorry if that's perfectly obvious!

AppDelegate is a class you need to create. Starting in Xcode with "New File...", and begin writing a class. It doesn't need to be called AppDelegate, just what makes sense to you, it does need to conform to NSApplicationDelegate, by adding the protocol to your .h file. If you write a method for the class called applicationWillTerminate, and (in IB) you connect the class to "File's Owner", then that method will automatically be called when the application closes.

NSUserDefaults is a class used for saving and loading of user preferences related data. Its pretty simple to use, almost identical to NSDictionary. If you can use NSDictionary, you'll be right at home with NSUserDefaults, just have a look at the documentation. The documentation built into Xcode is excellent. Just press Command-Option-Shift-? or use the Help menu.
 
Thank you! No time tonight, but this'll give me some more to work with! :apple:

PS — Yes I should consult Xcode's documentation. Thanks for the pointer. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.