如何在具有不同内容的控制器中多次使用自定义UIView?

时间:2015-09-29 20:23:48

标签: ios swift uiview

我为弹出窗口创建了自己的自定义UIView,我必须在屏幕上显示。我的UIView看起来像这样:

class HelpTipsPopover: UIView {

weak var title: UILabel!
weak var myText: UILabel!

override init(frame: CGRect) {
    super.init(frame: frame)

    let strongTitle = UILabel()
    title = strongTitle
    let strongMyText = UILabel()
    myText = strongMyText

    self.addSubview(strongTitle)
    title.translatesAutoresizingMaskIntoConstraints = false
    if selected == true{
    title.text = "Search"
    title.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
    title.textColor = UIColor.TRLMBlueBlackColor()


    let leftConstraint = NSLayoutConstraint(item: title, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 10)
    let topConstraint = NSLayoutConstraint(item: title, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 10)
    self.addConstraints([leftConstraint, topConstraint])

    self.addSubview(strongMyText)
    myText.translatesAutoresizingMaskIntoConstraints = false
    myText.text = "Search equities to view the order book and market prints for specific moments in time."
    myText.numberOfLines = 0
    myText.lineBreakMode = NSLineBreakMode.ByWordWrapping
    myText.font = UIFont(name: "Helvetica Neue", size: 12)
    myText.textColor = UIColor.TRLMBlueBlackColor()



    let leftDescription = NSLayoutConstraint(item: myText, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 10)
    let rightDescription = NSLayoutConstraint(item: myText, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1.0, constant: 10)
    let topDescription = NSLayoutConstraint(item: myText, attribute: .Top, relatedBy: .Equal, toItem: title, attribute: .Bottom, multiplier: 1.0, constant: 5)
    self.addConstraints([leftDescription, topDescription, rightDescription])

}

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

}

现在我有三个弹出窗口,我必须在我的视图控制器中显示每个弹出框中的不同标题和文本。以下是我的View Controller中显示弹出窗口的方法:

 func showPopover(){
    self.view.addSubview(helpTipsPopover)
    helpTipsPopover.tag = 1
    helpTipsPopover.translatesAutoresizingMaskIntoConstraints = false
    helpTipsPopover.layer.cornerRadius = 6
    helpTipsPopover.backgroundColor = UIColor(white: 1.0, alpha: 0.8)
    let leftConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute:       .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 10)
    let topConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .Top, relatedBy: .Equal, toItem: self.hotSpotOne, attribute: .Bottom, multiplier: 1.0, constant: 4)
    let widthConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
    let heightConstraint = NSLayoutConstraint(item: self.helpTipsPopover, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 80)
    self.view.addConstraints([leftConstraint, topConstraint, widthConstraint, heightConstraint])
    }


func showPopoverTwo(){
    self.view.addSubview(helpTipsPopover)
    helpTipsPopover.tag = 1
    helpTipsPopover.translatesAutoresizingMaskIntoConstraints = false
    helpTipsPopover.layer.cornerRadius = 6
    helpTipsPopover.backgroundColor = UIColor(white: 1.0, alpha: 0.8)
    let centerConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0)
    let topConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .Top, relatedBy: .Equal, toItem: self.hotSpotTwo, attribute: .Bottom, multiplier: 1.0, constant: 4)
    let widthConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
    let heightConstraint = NSLayoutConstraint(item: self.helpTipsPopover, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 80)
    self.view.addConstraints([centerConstraint, topConstraint, widthConstraint, heightConstraint])

}

现在我希望实现这样的目标:

enter image description here

所以每个popover应该有不同的标题和文本,但我想重用相同的UIView。任何帮助表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:2)

修改您的init方法

init(frame: CGRect, titleString: String, bodyString: String) {
  super.init(frame: frame)
  // Your current initializer

  title.text = titleString
  myText.text = bodyString

}

然后你可以初始化一个这样的弹出窗口:

let frame = CGRectMake(0,0,180,100)
let titleString = "My Custom Title"
let bodyString = "This is a body. I'm explaining things to you here."
helpTipsPopoverOne = HelpTipsPopover(frame: frame, titleString: titleString, bodyString: bodyString)

修改显示方法以将弹出窗口作为参数。然后你可以创建弹出窗口并显示你想要的任何文字!