Cocoa Fundamental Guide:这是最核心最基础的文档。介绍了最重要的概念
Cocoa Event Handling:Cocoa中如何处理事件
Introduction to Text System User Interface Layer,介绍了比如如何programatically 创建一个NSTextView,添加至NSScrollView,如何调整Text边距等等.
Introduction to Text Editing Programming Guide for Cocoa, 介绍如何使用Cocoa的Text System来完成编辑工作,比如key binding, 如何delegate message等.
一个简单界面的Cocoa Application:最最简单的一个Application.如果想学习一个最简单的Cocoa Application,从这个文档看起.
Model-View-Control模型
Cocoa中广泛使用了MVC这种的Design Pattern,把一个application功能分解为如下三个部分:
Model objects: 负责逻辑处理,不直接和用户界面打交道
View objects: 用户界面.比如label, textfield等
Controller objects: 连结Model和View, 比如处理界面送来的用户action,应用后台逻辑做出update等
内存管理
alloc, init, dealloc, retain, release, autorelease. 所有从NSObject继承而来的类都有这些方法,这些涉及到内存管理.
alloc: 从内存中分配一块内存,但仅仅是获得一块内存空间,未作任何初始化
init:初始化一个类,类似于C++中的构造函数,一般和alloc配合使用:
id myObject = [[NSString alloc] init];dealloc: 销毁一个类,释放内存空间
[myObject dealloc];retain: 将一个对象的引用计数加1
[myObject retain];release: 将一个对象的引用计数减1
[myObject release];autorelease:每当一个对象得到autorelease消息,这个对象就将被添加到autorelease pool当中. 当改eventloop结束时,所有autorelease pool当中的对象将被release.通常一个method要返回一个对象的时候,autorelease这个对象是个好主意.
- (NSString)autoreleasedString { NSString *string = [[NSString alloc] initWithString: @"The Sender of this message will never see this string…"]; [string autorelease]; // we have to do this or we'll get a memory leak return string; }
神秘的IBAction和IBOutlet?
经过预处理以后,IBAction实际上就是void而IBOutlet实际上根本就是一个空的宏(见定义在NSNibDeclarations.h).它们的作用是在于作为一种syntax sugar,提醒Interface Builder,"嗨,这几个被我修饰的变量或者函数是你应该知道的~".
事件处理(Event Handling)
有两种Message: 由外部设备比如键盘或者鼠标发起的Event message,以及由其他对象发起的Action message.
Event message 通常已知,所以NSResponder提供了声明以及缺省的实现.
NSResponder是一个抽象类.而Cocoa中最核心的三个类NSApplication, NSWindow, 以及 NSView都继承了NSResponder.
比如我们要处理mouseDown这个事件,我们可以继承一个NSView类(或者NSView的子类,比如NSTextView,NSTabView),然后重载mouseDown:这个方法。如果一个NSWindow上有多个NSView(或其子类), 则应该重载最上面的View,否则盖在底下的不能接受到该消息
- (void)mouseDown:(NSEvent *)theEvent { // determine if I handle theEvent // if not... [super mouseDown:theEvent]; }
Delegate
Common mistakes with delegation in Cocoa
一个在NSImageView中delegate mouseDown等消息的例子:
http://it.toolbox.com/blogs/macsploitation/adding-delegates-to-nsviews-21138
Text System
NSTextStorage 是NSMutableAttributedString的子类. 一般作为NSTextView 的text repository. Introduction to Text System Storage Layer Overview
NSTextView 和用户打交道的界面
NSTextContainer:Introduction to Text Layout Programming Guide
NSScrollView: Introduction to Scroll View Programming Guide for Cocoa
NSClipView:
NSParagraphStyle:Introduction to Rulers and Paragraph Styles
一个超级简单的Cocoa Text System Application
HelloWorld.h:
#importHelloWorld.m@interface HelloWorld : NSObject { NSWindow *window; NSTextStorage *textStorage; NSLayoutManager *layoutManager; NSTextContainer *textContainer; NSTextView *textView; } @property (assign) IBOutlet NSWindow *window; @end
#import "HelloWorld.h" @implementation HelloWorld @synthesize window; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application //setup textStorge textStorage = [[NSTextStorage alloc] initWithString:@"123abcde\n456"]; //setup layoutManager layoutManager = [[NSLayoutManager alloc] init]; [textStorage addLayoutManager:layoutManager]; [layoutManager release]; //setup textContainer NSRect cFrame = [[window contentView] frame]; textContainer = [[NSTextContainer alloc] initWithContainerSize:cFrame.size]; [layoutManager addTextContainer:textContainer]; [textContainer release]; //setup textView textView = [[NSTextView alloc] initWithFrame:cFrame textContainer:textContainer]; [window setContentView:textView]; [window makeKeyAndOrderFront:nil]; [textView release]; } @end
常用数据结构:
NSArray: 类似list, 不能添加元素
Objective-C Tutorial: NSArray读取index位置的元素
[myArray objectAtIndex:index]
NSMutableArray:一个类似python中list的存在.可以通过addObject:来添加元素.
NSDictionary
NSMutableDictionary:一个类似于python中dict的存在,提供setObject:forKey:和removeObjectForKey:
Do things Programatically
获取中文encoding
http://www.cocoadev.com/index.pl?CharacterEncoding
NSStringEncoding gb18030 = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);用中文encoding读文件
[options setObject:absoluteURL forKey:NSBaseURLDocumentOption]; [options setObject:[NSNumber numberWithUnsignedInteger:gb18030] forKey:NSCharacterEncodingDocumentOption]; [options setObject:NSPlainTextDocumentType forKey:NSDocumentTypeDocumentOption]; NSAttributedString *fileContents = [[NSAttributedString alloc] initWithURL:absoluteURL options:options documentAttributes:NULL error:outError];NSTextView背景设为透明
[[textView enclosingScrollView] setDrawsBackground:NO]; [textView setDrawsBackground:NO];NSImageView显示图片
NSImage *image = [NSImage imageNamed:@"yellow_background.bmp"]; [imageView setImage: image];给NSTextView和NSTextContainer之间添加空白区域
[myNSTextView setTextContainerInset:NSMakeSize (width, height)];使用NSLog,NSAssert输出信息,帮助调试
NSLog(@"init, %p, %p",backgroundImage, textView); NSAssert([myWindow delegate] == self, @"You forgot to connect the window delegate.!");NSLog 格式化字符串
其他
If you want to name your NSDocument subclass something other than MyDocument (the default name), change the name in Interface Builder and wherever it occurs in the Document header (.h) and implementation (.m) files. You must also change the name under the NSDocumentClass key in the Info.plist file.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextArchitecture/Tasks/TextEditor.html#//apple_ref/doc/uid/20001798-CJBHAJJJ
No comments:
Post a Comment