Objective-C 入門指南 - EncryptController.m




EncryptController.m 的原始程式碼如下

#import <Cocoa/Cocoa.h>
#import "EncryptController.h"
#import "Encrypt.h"

@implementation EncryptController

@synthesize encrypt, inputText, outputText, displayText;

- (IBAction) inputSomething: (id)sender {
    inputText = [inputField stringValue];
    NSString *temp = @"This is Input textfield. Your input is '";
    temp = [temp stringByAppendingString: inputText];
    temp = [temp stringByAppendingString: @"'"];
    [displayField setStringValue: temp];
 
    [temp autorelease];
}

- (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];
}

- (IBAction) loadEncrypt: (id)sender {
    BOOL isFileExist = [[NSFileManager defaultManager] fileExistsAtPath: @"../../encryptor"];
 
    if (isFileExist) {
        encrypt = [NSKeyedUnarchiver unarchiveObjectWithFile: @"../../encryptor"];
        [displayField setStringValue: @"This is Load button. Encrypt object is loaded."];
    }
    else {
        [displayField setStringValue: @"This is Load button. Encrypt object is not loaded."];
    }
}

- (IBAction) saveEncrypt: (id)sender {
    BOOL succeed = [NSKeyedArchiver archiveRootObject: encrypt toFile: @"../../encryptor"];
 
    if (!succeed){
        [displayField setStringValue: @"This is Save button. Encrypt object is not saved."];
    }
    else {
        [displayField setStringValue: @"This is Save button. Encrypt object is saved."];
    }
}

- (IBAction) encode: (id)sender {
 outputText = @"";
 
    if (inputText == nil || ![inputText compare: @""]) {
        [displayField setStringValue: @"This is Encode button. No input string!!"];
    }
    else {
        if (encrypt == nil) {
            [displayField setStringValue: @"This is Encode button. No Encrypt object!!"];
        }
        else {
            outputText = [outputText stringByAppendingString: [encrypt toEncode: inputText]];
   
            [outputField setStringValue: outputText];
            [displayField setStringValue: @"This is Encode button. The result is above."];
  }
 }
}

- (IBAction) decode: (id)sender {
    outputText = @"";
 
    if (inputText == nil || ![inputText compare: @""]) {
        [displayField setStringValue: @"This is Decode button. No input string!!"];
 }
 else {
        if (encrypt == nil) {
            [displayField setStringValue: @"This is Decode button. No Encrypt object!!"];
  }
        else {
            outputText = [outputText stringByAppendingString: [encrypt toDecode: inputText]];
   
            [outputField setStringValue: outputText];
            [displayField setStringValue: @"This is Decode button. The result is above."];
  }
 }
}

- (IBAction) clear: (id)sender {
    [inputField setStringValue: @""];
    inputText = @"";
    [outputField setStringValue: @""];
    outputText = @"";
    [displayField setStringValue: @"This is Clear button. Your input is clear."];
}

- (IBAction) copy: (id)sender {
    if (outputText == nil || ![outputText compare: @""]) {
        [displayField setStringValue: @"This is Copy button. No output string!!"];
 }
    else {
        NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; 
        [pasteBoard declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:nil]; 
        [pasteBoard setString: [outputField stringValue] forType:NSStringPboardType];
  
        [displayField setStringValue: @"This is Copy button. Result is copied to clipboard."];
 }
}

@end


您可以繼續參考
範例程式碼


相關目錄
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

沒有留言: