The Simplest App on MAC OSX

来源:互联网 发布:caffe 图像分割代码 编辑:程序博客网 时间:2024/05/16 03:22

Header:

////  myThirdTestAppAppDelegate.h//  myThirdTestApp////  Created by solaris_navi on 10/25/11.//  Copyright 2011 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>@interface myThirdTestAppAppDelegate : NSObject <NSApplicationDelegate> {    NSWindow *window;    NSTextField *op1;    NSTextField *op2;    NSTextField *answerField;}@property (assign) IBOutlet NSWindow *window;// get value from interface@property (assign) IBOutlet NSTextField *op1;@property (assign) IBOutlet NSTextField *op2;@property (assign) IBOutlet NSTextField *answerField;// action- (IBAction)addOp1AndOp2:(id)sender;@end

Function

////  myThirdTestAppAppDelegate.m//  myThirdTestApp////  Created by solaris_navi on 10/25/11.//  Copyright 2011 __MyCompanyName__. All rights reserved.//#import "myThirdTestAppAppDelegate.h"@implementation myThirdTestAppAppDelegate@synthesize window;@synthesize op1;@synthesize op2;@synthesize answerField;- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{    // Insert code here to initialize your application}- (IBAction)addOp1AndOp2:(id)sender {    float value1 = [self.op1 floatValue];    float value2 = [self.op2 floatValue];        float valueAnswer = value1 + value2;        [self.answerField setFloatValue:valueAnswer];}@end

What's the difficulties?

so basically, you have to understand how the messages will be send from view to controller. objective C Cocoa Applications use MVC framework, but unlike Java Applications, they will exchange messages instead of sending value directly from one object to another.

in this example, 

in the header file, we defined 2 outlets which connect to 2 textFields as operators and 1 label as answer field, and 1 action on the button which would be implemented an Add function(value1, value2) then sent the result back.

then, in the function file, in the action field of button, firstly, we ask for the value of 2 operators, therefore we send messages which look like [self.op1 floatValue]. floatValue/StringValue/doubleValue are built-in in the IDE, so we don't have to predefine them. finally, After applying the add function, we send message to answerField in self and tell it setFloatValue = the answer. 

what's more, if we define a class, we can create a property (member of class) in the class then use synthesize property to create get/set function (which is rather close to the way in VS.net, but maybe also accessible in Java although I don't think so).

after creating a class, don't forget to synthesize property after create an object from the class.  The class is in the self container as well.


 summarize all, it is hard to program in Mac OSX unless you got the idea of message-built-in object-oriented programming. but i think it is only a matter of time.. practice!


原创粉丝点击