|
SimpleTutorial
An extremely simple Cocoa# tutorial. Simple form with a textbox and a button.
AssumptionsFor this tutorial I will assume that you have OS X, Mono 1.1.8.1, Cocoa#, and Apple's Developer Tools installed. I'm also assuming that you're a novice at Apple Tools and using Cocoa#. I just switched from Windows to Apple 2 months ago. I had to figure all of this out the hard way :) Starting OffBuilding The NIBThe first thing to do is create a NIB file for use with the application. A NIB file is so much easier to use than creating the entire window and controls via code.
Actions vs. OutletsActions: Actions are like events. Creating an action in a nib will allow you to assign it to any control that has event capabilities. ''Note: You can also assign actions via code (ie "button1.action = buttonClick")''
Outlets: Outlets are a way to tie objects in the GUI to variables in code. If you don't have the outlet tied to anything then you can't use it. It's a way to tell the Code what variable is pointing to what interface element.
The CodeMain.csThe main.cs file is the entry point to the app. I split this off from the actual ""ApplicationController"" so that I can keep track of things easily. using System;
using System.Runtime.InteropServices;
using Apple.Foundation;
using Apple.AppKit;
class MainClass {
public void Run() {
Console.WriteLine ("initing: {0:x}", (int)Apple.Foundation.Class.Get("NSBundle"));
Application.Init();
Console.WriteLine ("initd");
Application.LoadNib ("Main.nib");
Application.Run();
}
static void Main(string[] args) {
MainClass main = new MainClass();
main.Run();
}
}The important thing to consider here is the "Application.LoadNib()" line. Make sure it's loading the proper NIB file. ApplicationController.csThe applicationcontroller.cs is the main interface handling point. This is where all the GUI elements are connected and defined. using System;
using System.IO;
using System.Collections;
using Apple.Foundation;
using Apple.AppKit;
[Register("ApplicationController")]
public class ApplicationController : NSObject {
[Connect]
public NSTextField textBox1;
[Connect]
public NSWindow mainWindow;
protected ApplicationController(System.IntPtr a, bool b) : base(a, b){}
//Form.Load Event
[Export("applicationWillFinishLaunching:")]
public void FinishLoading(NSNotification aNotification) {
textBox1.stringValue = "Form Loaded";
}
[Export("buttonClick:")]
public void buttonClick(object sender) {
textBox1.stringValue = "Button Pushed";
}
}Connect/RegisterI'm not sure what the technical explanation for this is, but this is the part that connects the variable you define to the object from the nib. You can have interface elements that do not have a local variable in the code, but you cannot connect local variables to non existant interface elements. Doing so will cause the app to crash on launch. For every interface element that you want to be able to access via code, you will need to create a new variable of the proper type and make sure that you use the "Connect" statement above it. The Register statement above the beginning of the class connects this class to the "ApplicationController" from the NIB. ExportThe export tag is the one that ties local events to the events declared in the NIB. You can assign these events to an interface object a couple of ways. You can Ctrl Click and drag from the interface element to the controller class, or you can define it via code. The NIB method is normally easiest but some interface elements don't like it. In the above example you can see that I have exported "buttonClick:" to my local buttonClick handler. The "applicationWillFinishLaunching:" event is generated by the (app/main window). Think of it like the FormLoad event in winforms (as far as I can tell). Helper FilesI create two more files just to kinda make my life easier. I create a makefile and then a command file. I create the command file so that I don't have to keep a terminal window open all the time. Makefileall: test test: Main.cs mcs -t:exe -out:SimpleTutorial.exe -pkg:cocoa-sharp *.cs rm -rf SimpleTutorial.app macpack -m:2 -n:SimpleTutorial -o:. -a:SimpleTutorial.exe -r:/Library/Frameworks/Mono.framework/ Versions/Current/lib/libCocoaSharpGlue.dylib -r:Main.nib I don't know much about makefiles, but the important parts in this file are anywhere the name "SimpleTutorial" is mentioned. Change this to whatever you want your program to be named. The -pkg option on the mcs command makes sure that it's compiled with the cocoasharp package. MacPack is the tool that creates the app bundles for OS X. Make sure you include any nibs/images/icons/etc as a resource by using the -r option of macpack. Command FileThis will give you a file you can just double click on to run the make file. Create this file in text editor and save it as <something>.command #!/bin/tcsh # if launched from the finder, we'll not be in the correct directory. # get to the directory where this script file lives cd `dirname $0` make FinishedWell, you should now be finished. Double Click on the <somthing>.command file and you should see everything compile successfully. If all went well you should see an OS app icon labeled SimpleTutorial. Double click on it, and you should see a new app running. If not, or if you find an error, please send an email to binary.god@gmail.com or comment in this wiki page. |
Sign in to add a comment
anus
anus shit
Apparently, the using apple. need to be changed to using Cocoa; ? this further presents this:
ApplicationController?.cs(7,38): error CS0246: The type or namespace name `NSObject' could not be found. Are you missing a using directive or an assembly reference?
This tutorial is inconsistent with the latest Interface Builder for OSX Leopard / Xcode 3.0. The window Step 4 refers to has been completely redesigned and it is not clear how to translate these steps for the new interface. I will gladly update here if I sort it out.
Same problem as justizin... Completely redesigned and no clue how to make it through the new interface.
Hope we can get something clarifying soon. Mono on the Mac can be an amazing thing.
Thanks for all the work you do.
Cheers.
After a while struggling with it it seems like with the new interface you have to drag and drop a "NSObject" (couldn't find nothing similar to a "SubClass? NSObject") from the controllers folder to the application's items. Then should we use the "Inspector" tool from the "Tools" menu to add the outlets, make the connections and all.
Just a guess for all of you to try as I haven't been able to run it completely due to errors in the code (something about an array declaration)... I will be looking at it when I have more time.
Hope it helps.
Thank you all.
Interface Builder for OSX Leopard / Xcode 3.0 step 7
"Instantiate ApplicationController?"
I don't know how to do this, so event are not connected Runtime error : "Could not connect the action buttonClick: to target of class NSObject"
tip: when you save the nib with Interface builder select nib 2.X since buttonClick: event is performClick: in 3.X
To insantiate an object in Interface Builder 3, do the following:
# Find the NSObject item in the Library palette (Command-Shift-L). # Drag it into the nib window, where the other objects reside. # Select the Identity tab in the inspector (Command-Shift-I). It's 'i', the second one from the right. # In the Class field under the Class Identity heading, enter the name of the class. # Optionally, you can set a name for the object in the same pane, using the Name field under the Interface Builder heading.
As far as I know, class definitions are now synchronized automatically. And when Control-dragging, a little black tool window pops up where you can select the outlet or action.
Right (or Option-) click any object to see all its available and used connections.
I can't get the markup to do what I want, hope it's clear though.
Hmmm i'm in trouble,
When i try to Compile i get :
ApplicationController?.cs(7,38): error CS0246: The type or namespace name `NSObject' could not be found. Are you missing a using directive or an assembly reference?
Any Idea To solve this problem ?
Hmmm Okay i try a lot of thing and i get an solution :
public class ApplicationController? : NSObject { ...
By public class ApplicationController? : Cocoa.Object { ..
And its seems working
Okay i made some advance. But i block on a little problem. i Can't Click Twice on the same button. if i reclick on a button? Mono generate an error :
System.ArgumentException?: Object type Cocoa.Object cannot be converted to target type: Cocoa.Window
And the Program Exit...
If anyone have an Idea ?