gpt4 book ai didi

xcode - Interface Builder/跨平台中的自定义 UIView

转载 作者:行者123 更新时间:2023-12-03 17:13:11 25 4
gpt4 key购买 nike

我有一个适用于 iOS 和 Mac OS 的控件库。其实现如下:

#if TARGET_OS_IPHONE

#import <UIKit/UIKit.h>

@interface MyCustonControl : UIControl
...
@end

#else

#import <Cocoa/Cocoa.h>

@interface MyCustomControl : NSControl
...
@end

#endif

如果我在 View Controller 中构建控件,则效果很好。如果我尝试使用 Interface Builder,我永远无法将 UIView 的自定义类设置为 MyCustomControl。它显示在列表中,我可以输入它,但如果我聚焦其他内容然后再次聚焦 View ,自定义类将恢复为 UIView。

我在两个实现中都实现了 initFromCoder,只是为了确保不是那样。

有办法解决这个问题吗? IB 很奇怪还是我应该以不同的方式定义我的类(class)?

最佳答案

您无法为控件设置自定义类,因为自定义类需要是 UIView/NSView 的子类,而 UIControl/NSControl 是 UIView/NSView 的父类。您应该将定义更改为

@interface MyCustomControl : UIView
...
@interface MyCustomControl : NSView

关于xcode - Interface Builder/跨平台中的自定义 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15275001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com