[英]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.