当前位置: 首页 > news >正文

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.

 

转载于:https://www.cnblogs.com/whyandinside/archive/2013/03/14/2959078.html

相关文章:

  • 用apache搭建web服务器
  • android: scrollbarStyle
  • LINQ 关键字
  • Linux 一次性杀死用户所有进程
  • 解决websphere6.1必须为元素类型web-app声明属性version
  • 将32位无符号整数表示的时间信号转化为习惯的形式
  • 财经法规与会计职业道德1
  • 截取字符串显示
  • Android TextView内容过长加省略号,点击显示全部内容
  • 一直令我纠结的问题,关于“线程”与“阻塞”
  • 随记:Linux下apache2.4.4的安装
  • 2012年最具影响力路由器配置精品文章荟萃【108篇】
  • 运算符重载03 -- = 和!
  • Linux下git安装
  • prototype.js 让你更深入的了解javascript的面向对象特性
  • ES6指北【2】—— 箭头函数
  • 07.Android之多媒体问题
  • ECS应用管理最佳实践
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • log4j2输出到kafka
  • nfs客户端进程变D,延伸linux的lock
  • php中curl和soap方式请求服务超时问题
  • python大佬养成计划----difflib模块
  • Rancher如何对接Ceph-RBD块存储
  • SpiderData 2019年2月25日 DApp数据排行榜
  • vue数据传递--我有特殊的实现技巧
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • 机器学习学习笔记一
  • 基于组件的设计工作流与界面抽象
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 设计模式(12)迭代器模式(讲解+应用)
  • 使用docker-compose进行多节点部署
  • 由插件封装引出的一丢丢思考
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​io --- 处理流的核心工具​
  • # include “ “ 和 # include < >两者的区别
  • # 数论-逆元
  • #图像处理
  • #预处理和函数的对比以及条件编译
  • $().each和$.each的区别
  • $L^p$ 调和函数恒为零
  • (1)(1.13) SiK无线电高级配置(六)
  • (2)STL算法之元素计数
  • (安卓)跳转应用市场APP详情页的方式
  • (九)One-Wire总线-DS18B20
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (转)jdk与jre的区别
  • .Net Core与存储过程(一)
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET 发展历程
  • .NET 分布式技术比较