有没有办法以编程方式让 UIView 实例继承 UIControl ?

发布于 2024-12-22 18:58:22 字数 628 浏览 5 评论 0原文

在许多 iOS 书籍和教程中,他们演示了如何在界面构建器中基本上将 UIView 转换为 UIControl 并让它继承新的更高级别的功能来响应点击并表现得像按钮一样。只需更改所选 UIView 的界面构建器中的类类型即可完成此操作。

这通常是为了在有人触摸给定视图控制器的“背景”或主视图时关闭键盘。一旦 UIView 的类更改为 UIControl,您只需在发生 TouchUpInside 事件时分配连接一个新的 IBAction 即可。

现在我理解了这个概念,但它看起来像是一个奇怪的 Xcode hack。原因是,我不知道如何以编程方式完成同样的事情。让我解释一下:如果我在代码中创建 UIView 的实例,并且希望它表现得像 UIControl,我将如何告诉这个 UIView 实例我希望它从 UIControl 继承。现在请记住,我实际上并不想子类化 UIControl,因为这会跳过更多的麻烦,而且我不认为这就是 Interface Builder 在更改该类类型时所做的事情,或者是吗?另外,请记住 UIControl 是一个抽象类,不能直接使用,否则我可能会这样做。

最终我的问题是,是否有一种等效的方法可以在纯代码中完成相同的事情并使 UIView 成为 UIControl 以便我可以为其分配更高级别的事件?

我希望这是有道理的,如果没有我可以详细说明。

In many iOS books and tutorials they demonstrate how in interface builder you can basically turn a UIView into a UIControl and have it inherit new higher level functionality to respond to clicks and behave like button for example. This is done simply by changing the class type in interface builder of a selected UIView.

This is often done to dismiss the keyboard when someone touches on the "background" or main view of a given view controller. Once a UIView's class is changed to a UIControl you just assign connect a new IBAction for when a TouchUpInside event occurs for example.

Now I understand this concept but it seams like a weird Xcode hack. The reason is, I don't see how I could accomplish the same thing programatically. Let me explain: If I create an instance of a UIView in code and I want it to behave like a UIControl how would I tell this UIView instance that I want it to inherit from UIControl. Now keep in mind I don't actually want to subclass UIControl because that would be jumping through more hoops and I don't think that is what Interface Builder is doing when you change that class type, or is it? Also, keep in mind UIControl is meant to be an abstract class and not to be used directly otherwise I would probably just do that.

Ultimately my question is, is there an equivalent way to accomplish the same thing in pure code and have a UIView become a UIControl such that I can assign the higher level events to it?

I hope this makes sense, if not I can elaborate.

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

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

发布评论

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

评论(2

何止钟意 2024-12-29 18:58:22

UIControl 继承自 UIView,因此如果您想以编程方式执行此操作,只需创建 UIControl 的实例即可。因此,例如,如果您以编程方式创建视图控制器的视图,您可以执行以下操作:

- (void)loadView
{
  CGRect viewSize = /* some appropriate default size */
  UIControl* myControlView = [[UIControl alloc] initWithFrame:viewSize];
  // configure your control (e.g., set events, etc)
  [self setView:myControlView];
  [myControlView release]; // omit this line if using ARC
}

UIControl inherits from UIView, so if you wanted to do this programmatically, you would just create an instance of UIControl instead. So if you were creating a view controller's view programmatically, for instance, you could do something like:

- (void)loadView
{
  CGRect viewSize = /* some appropriate default size */
  UIControl* myControlView = [[UIControl alloc] initWithFrame:viewSize];
  // configure your control (e.g., set events, etc)
  [self setView:myControlView];
  [myControlView release]; // omit this line if using ARC
}
短暂陪伴 2024-12-29 18:58:22

我创建了 xib 文件。然后新的 Objective c 类文件继承自
UIView.我在界面生成器中设置了 xib 类 Objective c 类,并且
然后将 Objective C 类的超类更改为 UIControl。

I created xib file. then new Objective c class file inherits from
UIView. i set xib class Objective c class, in interface builder and
then change super class of Objective c class to UIControl.

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