[英]Objective-C & iOS Dev
我刚刚开始使用Objective-C开发iOS,但在习惯语法方面遇到了一些麻烦。 我终于开始理解语义,甚至是理解它们的原因,但有一件事让我感到困惑。
在为iOS应用定义出口和动作时:为什么在接口声明中定义出口,而在接口声明之外定义动作和其他声明?
因为网点没有实现。 它们只是普通的旧实例变量。
另一方面,动作是方法(对象函数),需要实现。
方法实际上是在大括号内声明的,而ivars恰好在大括号内声明的。 @interface和@end之间的所有内容都在接口中。
@interface MyClass {
instance variables here. outlets are also instance variables
}
methods here. actions are also methods
@end
没有任何理由 。 这就是语法。 :)
IBOutlet
和IBAction
不是数据类型。 它们只是让Interface Builder知道哪些变量是出口,哪些方法是动作。
因此,当您编写此代码时:
@interface AClass
{
IBOutlet UIButton *someButton;
}
- (IBAction) buttonTap:(id)sender;
@end
编译时实际上最终是这样的:
@interface AClass
{
UIButton *someButton;
}
- (void) buttonTap:(id)sender;
@end
如果您更满意,实际上可以选择在属性声明中定义出口:
@interface SomeViewController : UIViewController
{
UITextField* aTextField;
}
@property (nonatomic, retain) IBOutlet UITextField* aTextField;
@end
IBAction和IBOutlet实际上只是接口构建器的线索,这里是实际的定义:
#ifndef IBOutlet
#define IBOutlet
#endif
#ifndef IBOutletCollection
#define IBOutletCollection(ClassName)
#endif
#ifndef IBAction
#define IBAction void
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.