The Simplest App on MAC OSX

2 篇文章 0 订阅

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!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值