Xcode confusion...weird results importing example code

Status
Not open for further replies.

spagthorpe

Ars Tribunus Angusticlavius
7,304
I decided that since I have a little time, I'd move away from the Windows world and start learning some Mac application development. Things have changed a bit since I was using Inside Mac in 85.

So, I find an open source project online. The Xcode project file doesn't really import right, so I create a new project, and pull in the source files. It builds, but when I run it, I get a small empty window. Obviously the call to NSApplicationMain() isn't going anywhere. Not understanding where that gets set up, I dig through the code. I find what is supposed to be the main loop. I trace it back through a couple of calls to:

Code:
@interface MyDriver : NSObject {
	IBOutlet Viewport *theDisplay;
	IBOutlet NSButton *theButton;
	IBOutlet NSMenu *fileMenu;
	IBOutlet NSWindow *theWindow;
}

- (IBAction)playGame:(id)sender;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;

- (void)windowDidResize:(NSNotification *)aNotification;

I don't really understand all the hoops at this point, but I'm still trying to figure out why 'playGame' isn't getting called.

I look at the info.plist file, and it looks normal. On a hunch, I go in to the project settings, and change the 'Product Name' to the name of the original open source app. It builds and runs like it is supposed to.

So what I'm trying to figure out, is the disconnect between something like the app name, and how that matters to the notification getting sent that runs the main loop. I've looked through all the code, and don't seem to find anything that looks like it's name dependent.
 
There are three entry points to start from if things don't appear to be running as expected (ApplicationMain isn't really worth looking at unless someone has gone out of their way to rename AppDelegate):

1 - if using Storyboards look at the .storyboard file referenced in the application plist, find the view controller marked "Initial View Controller" and then go look at the subclass.
2 - if not Storyboards, the plist probably refers to a .xib file - go look at the subclass implementing the view controller described in the .xib.
3 - if neither of those look at AppDelegate.m and you should find a view controller created and set as initial view in code.

I'm not exactly sure what changing the Product Name breaks but you can alternatively change the Project Name through Xcode's inspector view (keeps everything working because it will fix up all the references) or you can change "Bundle Display Name" to change what the app appears to be called on the device - first thing I ever put on the store had a Project and Product name of "Delegate Experiment" or some such thing, but the display name was what people thought it was called. These days as mentioned Xcode makes it much easier to rename a project. Fixing the broken product name issue you have is going to be somewhat similar to what people used to have to go through to manually change project name and fix all the broken references.

playGame is an IBAction so it is probably the target of some control described in a .xib file and will not be called unless you tap that button or whatever it is.
 

spagthorpe

Ars Tribunus Angusticlavius
7,304
'playGame' definitely gets called when the app starts up, as the title screen and everything else is in the main loop that ultimately gets called. Maybe there is an initial event sent somewhere that triggers it.

I am going to go read up on .xib files. There is one for the code, MainMenu.xib. I assumed it was just an interface template, and this application really doesn't use any Mac interface components. It actually just seems to do everything character based like a terminal application.

I can see other things I don't understand, like First Responder, Outlets and Reference Outlets mentioned in the Window setting. Both of those list the main driver code name in them. I obviously have zero clue what's going on with this.

Thanks.
 

Ember

Ars Centurion
346
Subscriptor++
I think you're using old project as starting point, and it's really bad. And no, if the project contains MainMenu.xib, it's used at runtime (at least compiled version of the xib file). Otherwise it's a bad sample that you should not look at.
There are many sample projects offered by Apple, use one of them

Better I'd recommend reading "Cocoa Programming for Mac OS X (4th Edition)" by Aaron Hillegass.
Cocoa framework is unique in many ways that it's really hard to understand just by studying sample source code. One of the reason is that it relies heavily on xib(nib) files which contains not only layouts of UI elements but also connections between UI elements and the code. You don't know where to look at, so you'll miss many important connections done inside xib files.

If you don't want to buy a book yet and want free documents and examples, I'd recommend starting with iOS first.
Apple provides better documents of iOS for starters than Mac, partly because iOS app is simpler and partly because they put more energy writing good guides and samples for iOS.
Start Developing iOS Apps Today
Your First iOS App
You can develop and run the code on simulator even if you don't have an iOS device.

Once you understand the basics of iOS frameworks, it would be easier to read the documents on desktop Cocoa and follow the sample codes.
 

spagthorpe

Ars Tribunus Angusticlavius
7,304
Good idea. I went through the first iPhone app tutorial. Read it at least, still waiting for 4.3.3 to download....slow connection. I'll work through more articles on the site, and also look for that book.

I can't say I'm really thrilled with Xcode. I've had it crash several times on me. I was running 4.2. Maybe the new version is better.
 
Status
Not open for further replies.