
EncryptController.m 的原始程式碼如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #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 *t 2 = [[encrypt cArray ] autorelease ]; NSString *temp = @"" ; int i; for (i = 0 ;i < 2 6 ;i++) { temp = [temp stringByAppendingString : [t 2 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
沒有留言:
張貼留言