IBOutlet 和 IBAction [英] IBOutlet and IBAction

查看:41
本文介绍了IBOutlet 和 IBAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Xcode 和 Interface Builder 中使用 IBOutlets 和 IBActions 的目的是什么?

What is the purpose of using IBOutlets and IBActions in Xcode and Interface Builder?

如果我不使用 IBOutlets 和 IBActions 会有什么不同吗?

Does it make any difference if I don't use IBOutlets and IBActions?

斯威夫特:

@IBOutlet weak var textField: UITextField!

@IBAction func buttonPressed(_ sender: Any) { /* ... */ }

目标-C:

@property (nonatomic, weak) IBOutlet UITextField *textField;

- (IBAction)buttonPressed:(id)sender { /* ... */ }

推荐答案

IBActionIBOutlet 是定义为表示可以在 Interface Builder 中引用的变量和方法的宏.

IBAction and IBOutlet are macros defined to denote variables and methods that can be referred to in Interface Builder.

IBAction 解析为 voidIBOutlet 解析为空,但它们向 Xcode 和 Interface builder 表示可以使用这些变量和方法在界面构建器中将 UI 元素链接到您的代码.

IBAction resolves to void and IBOutlet resolves to nothing, but they signify to Xcode and Interface builder that these variables and methods can be used in Interface builder to link UI elements to your code.

如果您根本不打算使用 Interface Builder,那么您的代码中就不需要它们,但是如果您打算使用它,那么您需要指定 IBAction用于将在 IB 中使用的方法和 IBOutlet 用于将在 IB 中使用的对象.

If you're not going to be using Interface Builder at all, then you don't need them in your code, but if you are going to use it, then you need to specify IBAction for methods that will be used in IB and IBOutlet for objects that will be used in IB.

这篇关于IBOutlet 和 IBAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆