
接著出現開啟檔案視窗,這裡要進入 demo 專案的資料夾,然後把 Encrypt.h 跟 Encrypt.m 都選起來,最後按 Add

嗯,這樣就完成加入 Encrypt.h 跟 Encrypt.m 囉

我們開始實作 EncryptController.m ,下面是雛型
#import <Cocoa/Cocoa.h>
#import "EncryptController.h"
#import "Encrypt.h"
@implementation EncryptController
@synthesize encrypt, inputText, outputText, displayText;
- (IBAction) inputSomething: (id)sender {}
- (IBAction) newEncrypt: (id)sender {}
- (IBAction) loadEncrypt: (id)sender {}
- (IBAction) saveEncrypt: (id)sender {}
- (IBAction) encode: (id)sender {}
- (IBAction) decode: (id)sender {}
- (IBAction) clear: (id)sender {}
- (IBAction) copy: (id)sender {}
@end這裡,我們先實作與 New 按鈕連結的 newEncrypt: 方法 (method) 。概念很簡單,建立完 Encrypt 物件 (object) 後,將該物件指派給屬性 (property) encrypt ,最後在 displayField 顯示提示訊息即可,如下
- (IBAction) newEncrypt: (id)sender {
encrypt = [[[Encrypt alloc] initRandomEncrypt] autorelease];
NSArray *t2 = [[encrypt cArray] autorelease];
NSString *temp = @"";
int i;
for (i = 0;i < 26;i++) {
temp = [temp stringByAppendingString: [t2 objectAtIndex: i]];
}
[displayField setStringValue: temp];
[temp autorelease];
}我們的 newEncrypt: 分成兩個部份,第一部分亦即建立 Encrypt 物件
encrypt = [[[Encrypt alloc] initRandomEncrypt] autorelease];
第二個部份在 displayField 顯示提示訊息,我們設計的稍微複雜一點,先取出 encrypt 的 cArray ,然後用迴圈讀取 cArray 的每個元素接到 NSString 中,也就是將密碼表的每個字元依順序拼成一個 NSString 字串,然後使用 displayField 的 setStringValue: 方法,將該 NSString 字串顯示在訊息欄
NSArray *t2 = [[encrypt cArray] autorelease];
NSString *temp = @"";
int i;
for (i = 0;i < 26;i++) {
temp = [temp stringByAppendingString: [t2 objectAtIndex: i]];
}
[displayField setStringValue: temp];
[temp autorelease];Build and Go ,點擊 New 按鈕後結果如下

接下來,我們來看看使用者的輸入與 TextField吧!
| 中英文術語對照 | |
|---|---|
| 類別 | class |
| 方法 | method |
| 物件 | object |
| 屬性 | property |
您可以繼續參考
GUI 篇
相關目錄
Objective-C 入門指南
Objective-C 教材
首頁
參考資料
Learning Objective-C: A Primer
The Objective-C Programming Language
Cocoa Fundamentals Guide
Coding Guidelines for Cocoa
Advanced Memory Management Programming Guide
Archives and Serializations Programming Guide
沒有留言:
張貼留言