How to structure the code for a Cocoa Application - objective-c

I'm writing a Cocoa application where I have an auto generated app delegate:(MyAppDelegate.h/MyAppDelegate.m)
So I am not sure of the best way to structure the class files for this Cocoa application. I understand MVC thoroughly (on the iPhone) but I'm having a block as to try organising the source properly in a Cocoa app.
I need to spawn two separate fullscreen OpenGL views. The problem is that I could simply create classes for "OpenGLView" then instantiate and call all this code into the app delegate, but it seems messy and it's aparently not the place to do it.
How would I best achieve the code structure?

Instantiate your application delegate in the MainMenu.xib file and hook it up. When I've done Cocoa fullscreen stuff, I've instantiated the views in the -applicationDidFinishLaunching method of the application delegate. It *is* messy, because for fullscreen views it doesn't really make sense to use interface builder. This is the same way that other folks do fullscreen apps in Cocoa.

Related

Designing app to load a UIViewController/SpriteKit app without going through XCode and interface builder?

Is it possible to create a SpriteKit game without the main storyBoard that get created by the interface builder ?
Because I'm using theos to make apps and it doesn't have an interface builder so everything have to be done by code
Yes, it's possible. You will just need to add all the views and sprites, etc in code, but it is possible.

Confused with how to implement view controller for cocoa app

I'm very new to objective-c and cocoa(started today), I have experience with java and this just seems like a different world. I'm building an app with scene kit and OSRSerialPort(Have looked at the example code and don't understand how the controller is connected to the view). The goal of this is to take accelerometer data from a micro controller and making a visual representation of it with scene kit. I've tried to implement my controller in the same way as the example project in OSRSerialPort's github. Mine just doesn't seem to work. I'm just confused on why my project would don't receive input from the port but the example one receives it just fine when I feel as I've implemented it the correct way. I've attached the xCode project here.
Would love any help on this.
I'm building an app with scene kit and OSRSerialPort(Have looked at
the example code and don't understand how the controller is connected
to the view)
With Xcode, you can connect a Controller to a View without writing any code. When you use Xcode's Interface Builder(IB), almost non-readable xml data is inserted automatically into the .xib file for a window, which is the code that actually connects the two objects.
Then when your application starts, the .xib file is parsed, and the objects listed in the .xib file are created and the connections listed in the .xib file are setup.
I would guess that the connections you can't find are in the window's .xib file.
In fact, ORSSerialPort's README.md says this:
This application demonstrates that it is possible to setup and use a
serial port with ORSSerialPort without writing a lot of "glue" code.
Nearly all of the UI is implemented using Cocoa bindings.
without writing a lot of "glue" code means that instead of writing code the developers did a lot of dragging between objects in IB. And Cocoa bindings is a term of art, but you'll see that you have to understand other things first.
After figuring out how to download the project and open it locally in Xcode, I was able to look at MainMenu.xib in IB. Below is a screen shot of what MainMenu.xib looks like in IB, and on the right hand side there is a partial list of the connections for ORSSerialPortDemoController:
Those connections were established without writing any code.
I'm very new to objective-c and cocoa(started today)
Well, then put your current project aside, and learn the basics of how to use Xcode in Hello World style apps, then work your way up. As you progress in your learning, you will know when you are ready to go back to your current project.

Application Delegate Usage

I'm fairly new to Objective-C and cocoa programming, so I don't really understand the concept of App Delegates.
When we create a cocoa application, do we store our code (Methods, actions, outlets) in the App Delegate files or do we create a new file that will act as a controller and code from there. Right now, I put all of my code in those two files, but from what I read, your goal is to try to make your App Delegate files as slim as possible.
My question is: What's the usage of the app delegate files?
Talking about applicationDidFinishLaunching::
It's just your application entry point. Normally you only create the window and your first ViewController, or your Tabbar - your main starting interface class - here.
All the other delegate methods of the NSApplicationDelegate have other functions of course. Most of them are the point, where you react on the state of the app. Opened / Closed / Backgrounded / Reopened etc.
But you should probably have a look at the programming tutorials in the iPhone documentation. There is a lot of information on how to structure your objc projects. E.g. look here: Start Developing iOS Apps Today
Or if your looking for OSX Apps, look here:
1) Your First Mac App
2) Mac App Programming Guide
There is also a bunch of Sample code.
The App Delegate is a handler location to handle events that occur on the application. Things like open and close. It also hangs around the whole time the application is executing and you can grab the singleton instance at any point by doing [[NSApplication sharedApplication] delegate].
This comes in handy for handing objects between controllers and serving as a router for events. You can also store some data on the delegate if you need to modify/have access to it in different parts of the code.
This all works well for simple applications, but as things become more complex, you need to have some division of responsibilities. The AppDelegate should really only be responsible for actions that occur on the application itself, not on another view or a controller. Putting all/most of your code in the AppDeligate is certainly bad practice and will lead to horrible code as things get more complex and need to be maintained.

Connecting App Delegate to Controller Class

I'm stuck on this step of developing an XCode application: https://developer.apple.com/library/mac/#documentation/General/Conceptual/Mac101/Articles/07_WhereNext.html
What I'm confused about is the role of the AppDelegate class that's created by default when creating a project initially in XCode 4. If I implement a custom controller, let's use the example link above and call it TrackController. Do I make that controller the app delegate? It seems any tutorial I read isn't very clear on this.
So, in AppDelegate, do I create a new instance of the controller class here? And if so, do I then hook up the outlet here? Or do I change the File's Owner to be the controller class? If that's the case, then how's that done in XCode 4?
Not a very well defined question I know, but I'm sure someone knows what I'm talking about.
EDIT
The following screenshot shows what I mean. The blue box is now "Track Controller". I've shown the AppDelegate class in this case though to make it clear. What do I use, AppDelegate.m, or TrackController.m? How should it be done?
EDIT 2
Going back to this, I've uploaded the code to GitHub here as I still haven't figured out how to hook everything up.
To re-explain, I need to figure out how AppDelegate and TrackController all relate and communicate with one another. The concepts are generally new to me and I've read the Apple documentation but they haven't really helped me. I've even had in-depth discussions with experts and I still fail to see how it should work.
My model is Track, then I have TrackController and of course AppDelegate. AppDelegate is instantiating TrackController and I was hoping that it'd be able to open up its window.
What am I doing wrong? Hopefully an expert can help me!
What it's describing is that the application delegate should focus on handling the delegate calls sent to the app.
You actually create a new class to act as the primary controller of the app, and then you add code to call it when the app is finished loading. From there on out, that app is (supposedly) your primary control point (except, of course, when it 'hands off' to another class).
I'm more familiar with how the process works on iOS than Macs, which have to worry about windows, but for an iOS app you'd add code to call your starting view controller from inside ApplicationDidFinishLaunching:WithOptions:. From there, the view controller takes off, and the easiest way to view the entire process is that you start at the view controller (that's how most tutorials handle it, in fact, letting XCode handle the fact that the app delegate creates that view controller).

Coming from iOS to OS X - where do I put my code?

I'm an iOS developer just starting to develop for OS X. In iOS, I always follow the MVC pattern, and put appropriate code in UIViewController subclasses. The App Delegate simply initializes the main view controller, and that's it. After that the view controllers handle all logic.
But in OS X, I seem to put everything in the app delegate. My app delegate class is now about 300 lines long and I am wondering how I can follow the same pattern I did in iOS. There are no view controllers!
What I mean is, what is the common accepted way to organize code for Mac Application?
You can create your own controller classes, and put an instantiation of these controller classes between the app delegate or model objects, and your view objects. Just because there is no pre-built view controller class, does not mean that you can't make your own similar partition, by creating custom controller classes of your own, as needed.