设为首页 收藏本站
查看: 825|回复: 0

[经验分享] Cocoa.Programming.for.Mac.OS.X 3rd 前8章小知识点

[复制链接]

尚未签到

发表于 2015-12-30 14:49:42 | 显示全部楼层 |阅读模式
  最近看了《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的理解。
DSC0000.png
  首先,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.
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-158505-1-1.html 上篇帖子: How to set the JAVA_HOME variable in Mac OS X – Snow Leopard 下篇帖子: 如何在mac os中安装gdb及为gdb进行代码签名
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表