IB出口混乱

发布于 2024-12-28 17:25:26 字数 187 浏览 1 评论 0原文

从 Java/Android 迁移到 iOS/Objective-C 后,我经历了一些困惑阶段,无法理解或找到任何可行的解释:

  • 什么是 IBOutlet?
  • 它有什么用呢?
  • IBoutlet代码是否必须在每个实现文件中定义或者 头文件?

有没有和java类似的东西?

I'm going through some confusion phase after moving from Java/Android to iOS/Objective-C and can't understand or find any viable explanation on the following:

  • What is IBOutlet?
  • What use does it have?
  • Does IBoutlet code have to be defined in each implementation file or
    header file?

Any similiar stuff to what we have in java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野味少女 2025-01-04 17:25:26

IBOutlet

假设您在 .xib 接口文件中添加了一个 UILabel,并且您想要稍后在运行时更改标签的文本,则必须以某种方式引用它。
通过添加 IBOutlet 关键字,您基本上告诉您的 .xib 文件您想要将现有的 UILabel 连接到此属性。
Interface Builder 将识别并让您能够将标签连接到您的代码。

@property (unsafe_unretained, nonatomic) IBOutlet UILabel *label;

但是,如果您不想在从 .xib 初始化标签后更改标签,则无论如何都不必创建属性。

IBAction

同样的事情也适用于 UIButton 上的函数。
例如,如果您在 .xib 文件中添加了一个按钮并希望在按钮单击事件上触发一个函数,您可以像这样声明您的函数。

- (IBAction)buttonClicked:(id)sender;

同样,IBAction 关键字只是告诉 Interface Builder 您希望将此函数连接到 .xib 文件内的任何内容(在我们的例子中为按钮)。

顺便说一句,简单地将某些内容声明为 IBOutlet 或 IBAction 不会发生任何事情。您总是必须返回 Interface Builder 并根据需要连接所有内容。

IBOutlet

Say you added a UILabel to your .xib Interface file and you e.g. want to change the text of the label later on during runtime, you'd have to reference it somehow.
By adding the IBOutlet keyword you basically tell your .xib-file that you want to connect an existing UILabel to this property.
Interface Builder will recognize, and gives you the possibility to wire up the label to your code.

@property (unsafe_unretained, nonatomic) IBOutlet UILabel *label;

However, if you never want to change the Label after it was initialized from the .xib, you don't have to create a property anyway.

IBAction

The same thing would apply to functions on a UIButton.
E.g. if you added a button in your .xib-file and want to trigger a function on the button clicked event, you'd declare your function like this.

- (IBAction)buttonClicked:(id)sender;

Again, the IBAction keyword simply tells Interface Builder that you'd like to wire this function to anything inside the .xib-file (in our case the button).

Btw, nothing happens by simply declaring something as IBOutlet or IBAction. You'd always have to go back into Interface Builder and wire everything up as desired.

若水微香 2025-01-04 17:25:26

由于您在 Android 方面经验丰富,我确信您已经使用资源文件来引用子视图,例如项目中某个 .xml 视图的按钮或图像视图。

例如:

ImageView imageView = (ImageView) findViewById(R.id.myimageview);

虽然此属性会自动链接到 Android 中的资源文件,但您的 iOS 项目不会这样做。相反,您可以通过创建新的 IBOutlet 属性并通过 Interface Builder 链接它来将子视图直接链接到专用 .h 文件。

IBAction 也是如此。虽然您可以直接引用按钮并通过添加 onClick 侦听器

Button button = (Button) findViewById(R.id.mybutton);
button.setOnClickListener(this);

来接收“this”的 onClick 方法中的操作,但这在您的 iOS 项目中是不同的。再次,您将按钮触发的操作直接链接到专用 .h 文件,并使用 IBAction 标记来自 XIB 文件的操作(请参阅 alex 的回答)。
正如 Mark Adams 所说,IBOutlet 和 IBAction 只不过是一个 void typedef,只能由编译器使用。

As you are experienced in Android I am sure you have used the resources file to reference subviews like buttons or image views of a certain .xml-view within your projects.

For example:

ImageView imageView = (ImageView) findViewById(R.id.myimageview);

While this properties will automatically by linked to the resources file in Android, this will not be done for your iOS projects. Instead you link subviews directly to the dedicated .h-file by create a new IBOutlet property and link it via the Interface Builder.

Same for IBAction. While you can reference a button directly and add an onClick listener via

Button button = (Button) findViewById(R.id.mybutton);
button.setOnClickListener(this);

to receive the action in the onClick methode of "this", this is different in your iOS project. Again, you link the action which will be triggered by the button directly to the dedicated .h-file and mark the action coming from XIB-file with IBAction (see answer by alex).
As stated by Mark Adams, IBOutlet and IBAction is nothing more than a void typedef and will only be used by compiler.

○闲身 2025-01-04 17:25:26

IBOutlet 只是编译器能够在 .xib 文件和头文件之间创建链接的“提醒”。它与 void 类似,但需要创建与任何接口生成器文件的连接

The IBOutlet is just a "heads up" for the compiler to be able to create links between .xib files and header files. It is similar to void, but needed to create the connections to any interface builder file

你又不是我 2025-01-04 17:25:26

IBOutlet:如果将变量声明为 IBOutlet,则将该变量放入 Interface Builder 中类的 Outlet 中。换句话说,您有机会使用 Interface Builder(从现在开始为 IB)将该变量连接到组件(当然具有相同的类)。

IB还有一个特殊的词:IBAction。如果将方法的返回类型声明为 IBAction,则将该方法作为可由 Interface Builder 分配给按钮触摸或其他操作的操作。不需要返回任何特殊值,只要假设返回值为void即可。

如果您是 iOS 新手,请注意:使用 Interface Builder 时要小心,因为必须由 Interface Builder 实例化的所有变量(例如自定义 UILabel 或其他)都将使用 initWithCoder 方法而不是 init 或 initWithNibName 进行实例化。我确实为此浪费了很多时间!

我希望它有帮助。

IBOutlet: If you declare a variable as IBOutlet you are putting that variable in the Outlet of the class in Interface Builder. In other words you have the chance for connect that variable to a component (with the same class, of course) using the Interface Builder (from now IB).

There is another special word for IB: IBAction. If you declare the return type of a method as IBAction, you are putting that method as action that can be assinged by Interface Builder to a button touch up or other actions. There is not nescessary to return any special value, just assume that return value is void.

If you are new in iOS, just an advice: be careful using the Interface Builder because all variables that must be instantiated by Interface Builder (like a custom UILabel or what ever) will be intantiathed with the method initWithCoder instead of init or initWithNibName. I did loose a lot of time with that!

I hope it helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文