简体   繁体   English

Objective-C和iOS开发

[英]Objective-C & iOS Dev

I just started developing iOS using objective-c, and I've had some trouble getting used to the syntax. 我刚刚开始使用Objective-C开发iOS,但在习惯语法方面遇到了一些麻烦。 I'm finally beginning to understand the semantics- even the reason for them, but one thing is puzzling me. 我终于开始理解语义,甚至是理解它们的原因,但有一件事让我感到困惑。

When defining the outlets and actions for an iOS app: why are the outlets defined within the interface declaration, versus the actions and other declarations defined outside the interface declaration? 在为iOS应用定义出口和动作时:为什么在接口声明中定义出口,而在接口声明之外定义动作和其他声明?

Because outlets have no implementation. 因为网点没有实现。 They are just plain old instance variables. 它们只是普通的旧实例变量。

Actions, on the other hand, are methods (object functions) and they need to be implemented. 另一方面,动作是方法(对象函数),需要实现。


Methods are declared actually, but outside the curly brackets, while ivars happen to be declared inside the curly brackets. 方法实际上是在大括号内声明的,而ivars恰好在大括号内声明的。 Everything between @interface and @end is in the interface. @interface和@end之间的所有内容都在接口中。


@interface MyClass {
   instance variables here. outlets are also instance variables
}

methods here. actions are also methods
@end

There is no reason why . 没有任何理由 This simply is the syntax. 这就是语法。 :) :)

IBOutlet and IBAction are not data-types; IBOutletIBAction不是数据类型。 they're just for Interface Builder to know which variables are outlets and which methods are actions. 它们只是让Interface Builder知道哪些变量是出口,哪些方法是动作。

So, when you write this: 因此,当您编写此代码时:

@interface AClass
{
    IBOutlet UIButton *someButton;
}

- (IBAction) buttonTap:(id)sender;

@end

It actually ends up being something like this when compiled: 编译时实际上最终是这样的:

@interface AClass
{
   UIButton *someButton;
}

- (void) buttonTap:(id)sender;

@end

You actually have the option of defining the outlets in a property declaration if you're more comfortable: 如果您更满意,实际上可以选择在属性声明中定义出口:

@interface SomeViewController : UIViewController 
{
    UITextField*    aTextField;

}
@property (nonatomic, retain) IBOutlet UITextField* aTextField;

@end

IBAction and IBOutlet are really just clues to interface builder, here are the actual definitions: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM