2016-08-16 101 views
-2

在Identity Inspector中,我想为我的UIButton设置自定义类。 因此,我创建了我的自定义类,它是一个UIView,但我的自定义类未出现在列表中。 反之亦然,如果我添加一个UIView,列表中出现的大多数类别包括UIButtonUIButton的自定义类视图

如果UIButtonUIView一个子类,而不是UIViewUIButton, 为什么是可能的,为什么我不能用我的UIView自定义类代表我的UIButton

+0

我不会推荐子类UIButton的,但如果你无论如何要做到这一点,创建你customClass从UIButton的,而不是UIView的继承。然后它会出现在身份检查员 – NSNoob

回答

2

我并不完全确定你的意思,但我会走了。

你的问题......

如果UIButton的是的UIView的一个子类,而不是一个UIView是一个UIButton,它为什么可以,为什么我不能用我的UIView自定义类代表我的UIButton。

我相信你说的话...

  1. UIButtonUIView一个子类。
  2. 您的自定义视图是UIView的一个子类。
  3. 为什么不能将您的UIButton设置为您的自定义视图?

是吗?

如果是这样,这是相当清楚的。仅仅因为他们来自同一个班级并不意味着他们是相关的。 A UILabel也衍生自UIView,但有很大不同。

如果你想创建的UIButton自定义子类,然后自定义类应该从UIButton派生...

你应该有......

@interface YourSubclass: UIButton 

而不是...

@interface YourSubclass: UIView 

这样做会使自定义子类出现在Interface Builder中的Identity Inspector中。

+0

好吧,我明白,并在UIView列表中显示UIButton,因为它是UIView的子类。对不起,对于简单的问题,我是一个新手 – ndPPPhz

+1

@ndPPPhz不要对不起:)每个人都必须学习:) – Fogmeister

1

您可以为UIButton创建自定义子类。然后将其设置为任何按钮的自定义类。

class CCButton: UIButton { 

    /* 
    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func drawRect(rect: CGRect) { 
     // Drawing code 
    } 
    */ 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     createBorder() 
    } 

    required override init(frame: CGRect) { 
     super.init(frame: frame) 
     createBorder() 
    } 

    private func createBorder(){ 
     //Define Layer 
     self.layer.cornerRadius = 8.0 
     self.layer.masksToBounds = true 
    } 
}