zoukankan      html  css  js  c++  java
  • Cocoa.Programming.for.Mac.OS.X 3rd 前8章小知识点

    最近看了《Cocoa.Programming.for.Mac.OS.X 3rd》前八章,虽然第一遍的时候,可以很容易按照书中的介绍把例子程序正确的运行起来,但是并不知道为啥那样写代码就work了。对于一些隐藏的知识点,不便作为单独的文章来介绍,本文收集这些知识,介绍如下。

    1. NSUserInterfaceItemIdentification Protocol 

    The NSUserInterfaceItemIdentification protocol is used to associate a unique identifier with objects in your user interface. The protocol is adopted by AppKit interface objects to support window restoration, whereby information about window and other interface-related objects is preserved and used to restore the application’s interface during the next launch cycle.

    Identifiers are used during window restoration operations to uniquely identify the windows of the application. You can set the value of this string programmatically or in Interface Builder. If you create an item in Interface Builder and do not set a value for this string, a unique value is created for the item when the nib file is loaded. For programmatically created views, you typically set this value after creating the item but before adding it to a window.

    You should not change the value of a window’s identifier after adding any views to the window. For views and controls in a window, the value you specify for this string must be unique on a per-window basis.

    要遵守NSUserInterfaceItemIdentification协议,需要实现identifier这个Property。其设计目的是用于window restoration,但是并不限于此,比如在第八章最后的Challenge 2中,我们用它来与某个key关联,从而实现类似于绑定的功能:

    -(id)tableView:(NSTableView *)aTableView
        objectValueForTableColumn:(NSTableColumn *)tableColumn 
                              row:(NSInteger)row
    {
        NSString *identifier = [tableColumn identifier];
        MSPerson *person = [employees objectAtIndex:row];
        return [person valueForKey:identifier];
    }
    
    -(void) tableView:(NSTableView *)aTableView
       setObjectValue:(id)object 
       forTableColumn:(NSTableColumn *)tableColumn 
                  row:(NSInteger)row
    {
        NSString *identifier = [tableColumn identifier];
        MSPerson *person = [employees objectAtIndex:row];
        [person setValue:object forKey:identifier];
    }

     2. 第八章中NSArrayController如何工作的。

    MVC是一种常见的设计模式,看看NSArrayController如何工作的,加深我们对Cocoa中MVC的理解。

     首先,NSArrayController是一个Controller,将Model(MyDocument的employees)和View连接起来了。其中跟Model的联系是使用了Binding,将NSArrayController的Content Array绑定到File Owner's employees。而View中一个column用于显示人名,一个用于显示expectedRaise,它们用的也都是Binding,其使用的是Controller Key[???]加Model Key Path的方式来实现的。而View中的两个button的target是NSArrayController的add和remove函数。

    这一部分其实没有涉及什么内幕,之所以特别提出来说,就是我在使用datasource的方式实现RaiseMan时,尝试使用Binding,可是却不起作用!!接下来就要根据一下问题进行探索:

    1. Interface Builder中对各种UI element的Binding原理是什么,应该在什么情况下使用?

    2. <待定>

    3. Number Fromatter是如何工作的

    先了解Number Fromatter,在Foundation中定义了抽象类NSFormatter,它声明了一个用于create, interpret和validate 文本内容的对象的接口。并且Foundation中还提供了其两个子类实现:NSNumberFormatter 和NSDateFormatter。需要注意的一点是,Fromatter完成的任务是实现string<->object的双向转换,以及验证string是否valid。

    In Cocoa, user interface cells that display text but have an arbitrary object as their content can use formatters for both input and output. When a cell is displayed, the cell converts an arbitrary object to a textual representation. How a cell displays the object depends on whether or not the cell has an associated formatter. If a cell has no formatter, the cell displays its content by using the localized representation of the object. If the cell has a formatter, the cell obtains a formatted string from the formatter. When the user enters text into a cell, the cell converts the text to the underlying object using its formatter.

  • 相关阅读:
    html5文件api
    折腾一个自己的UrlRewrite
    hdu 4218 ( IMBA? )
    hdu 4217 Data Structure
    九度OJ 1008
    倒酒趣题详解
    第三届蓝桥杯复赛原题
    第三届蓝桥杯复赛题解析
    hdu 4223 Dynamic Programming
    hdu 4224 Enumeration
  • 原文地址:https://www.cnblogs.com/whyandinside/p/2959078.html
Copyright © 2011-2022 走看看