iOS自学笔记--outlet, @property

简介:

照猫画虎一两个程序后。也就是画了几个控件,可能会对例子中的一些陌生玩意儿产生疑惑,比如,什么是outlet.

什么是outlet?

Apress的书有这么一句话:

Absolutely right. Our controller class can refer to objects in the nib file by using a special kind of instance variable called an outlet. Think of an outlet as a pointer that points to an object within the nib. For example, suppose you created a text label in Interface Builder and wanted to change the label’s text from within your code. By declaring an outlet and connecting that outlet to the label object, you could use the outlet from within your code to change the text displayed by the label.

outlet是个特殊的类成员变量,可以看成是指针,指向一个nib中的对象。

说俗点就是,你画出来一个text label. 假设想用代码来编辑这个label控件。你需要声明一个outlet然后和这个label控件的对象(object) 联系起来(指针)。

Apple的文档中称:

IBOutlet is a special keyword that is used only to tell Xcode to treat the object as an outlet. It’s actually defined as nothing so it has no effect at compile time.

其实也就是说,IBOutlet其实只是告诉Xcode,让Interface builder(就是拖控件的环境)知道某个对象是指向某个控件的。编译期间压根没用。


最后的总结就是,outlet只是用来告诉Xcode某个指针变量指向画出来的某个控件,然后在代码中使用那个指针来操作那个控件。


什么是 @property?

一会儿看书,一会儿又看Apple的文档,会发现有很多不一样的地方。这很正常,技术在更新,标准在变化。比如最新的apple的文档(基于iOS 5)的,从来不定义类变量(instant variables)。只是在@end前定义一个@property。而很多书中的例子还在定义instant variable(比如Apress的iPhone 4开发)

@property(头文件中) 是用来和@synthesize(源文件中) 一起告诉编译器(compiler)在编译的时候自动生成getter和setter方法,省事儿用的。

其实Objective-C 2.0 建议大家只使用@property忽略类成员变量的定义。 然而,这取决于具体运行环境(老版本可能不太支持,具体不管了,反正搞明白问啥有的只用@property有的同时声明类成员变量就行了)。

相关文章
|
6月前
|
Unix 开发工具 iOS开发
iOS应用逆向工程笔记 -1
iOS应用逆向工程笔记 -1
36 0
|
6月前
|
编译器 iOS开发 异构计算
读iOS核心动画笔记
读iOS核心动画笔记
26 0
|
1月前
|
iOS开发
iOS自动混淆测试处理笔记
iOS自动混淆测试处理笔记
12 0
|
2月前
|
iOS开发
  iOS 自动混淆测试处理笔记
  iOS 自动混淆测试处理笔记
|
5月前
|
iOS开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)(3)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)
59 0
|
5月前
|
前端开发 数据处理 iOS开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)(2)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)
48 0
|
5月前
|
iOS开发 Kotlin 容器
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)(1)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(五)
53 0
|
5月前
|
存储 缓存 前端开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(四)(2)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(四)
50 0
|
5月前
|
前端开发 Swift iOS开发
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(四)(1)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(四)
94 0
|
5月前
|
存储 PHP Swift
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(三)(2)
实战编程·使用SwiftUI从0到1完成一款iOS笔记App(三)
67 0