带有 UIButtons 的下拉列表的 UIView class - delegate/protocol 问题?

UIView class for drop down list with UIButtons - delegate/protocol issue?

我正在尝试在 ViewController 中创建自定义下拉列表。将有 5 个下拉列表,每个列表将有 4 个选项。由于列表的数量,我决定制作一个 UIView,其中每个列表都有 UIButtons 形式的四个选项。现在我只是想弄下来一个;因此,以下代码适用于包含五个选项的一个下拉列表(包括选定的一个,我将在下面进一步解释)。

基本上我想要的是有一个显示所选值(或启动时的默认值)的按钮,然后当您单击该值时,将显示包含 4 个按钮(也称为下拉列表)的 UIView在原始按钮下方。当用户单击其中一个按钮时,我希望具有所选值的按钮具有被单击按钮的标题。

我遇到以下问题:

  1. 我希望能够将 ViewController 中的四个按钮的标题传递给 UIView,因为我想多次使用此 UIView 并为四个按钮。我不知道如何将值传递给 UIView class.

  2. 当单击下拉列表中的一个选项(即 UIButton)时,我不知道如何将按钮标题的值从 UIView 传回 UIViewController。我尝试将标题设置为 ViewController 中的一个变量,但这没有用(显示为 nil)。

提前非常感谢 - 我知道这是一个很长的问题,我真的不确定这是否是一个很好的方法来实现我正在尝试做的事情,但它在我的脑海中是有意义的。

这是我的 ViewController

代码
    var buttonsLeft: buttonsView = buttonsView() // this is the UIView subclass

    var time   = UIButton.buttonWithType(UIButtonType.System) as! UIButton

    override func viewWillAppear(animated: Bool) {

    //hidden drop down list
    self.buttonsLeft.frame = CGRect(x: self.view.bounds.width*(1/6) - 50, y:120, width:100, height: 135)
        self.buttonsLeft.hidden = true

    //button with selection showing or the default value at launch
    self.time.frame = CGRectMake(self.view.bounds.width * (1/6) - 50, 90, 100, 30)
        self.time.setTitle("1 DAY", forState: UIControlState.Normal)
        self.time.addTarget(self, action: "showLeft", forControlEvents: UIControlEvents.TouchUpInside)
        self.time.hidden = false
        self.view.addSubview(self.time)
    }  
   //this function shows the list
   func showLeft(){
        self.view.addSubview(self.buttonsLeft)
        self.buttonsLeft.hidden = false
    }

这是 UIView buttonsView 的代码:

import UIKit

class buttonsView: UIView {

var option1 = UIButton()
var option2 = UIButton()
var option3 = UIButton()
var option4 = UIButton()
var buttons: Array<UIButton> = Array()
var title:String = String()


override init(frame: CGRect) {

    super.init(frame: frame)
    self.buttons = [option1, option2, option3, option4]
    self.option1.setTitle("1 DAY", forState: UIControlState.Normal)
    self.option2.setTitle("1 MONTH", forState: UIControlState.Normal)
    self.option3.setTitle("1 YEAR", forState: UIControlState.Normal)
    self.option4.setTitle("LONGER", forState: UIControlState.Normal)

    var yStep = 35
    for var i:Int = 0; i  < 4; ++i {
        var totalY:CGFloat = CGFloat(i*yStep)
        buttons[i].frame = CGRectMake(0, totalY, 100, 30)
        buttons[i].addTarget(self, action: "choseOption:", forControlEvents: UIControlEvents.TouchUpInside)
        buttons[i].hidden = false
        self.addSubview(buttons[i])
    }

}


func choseOption(sender:UIButton){
    self.title = sender.titleLabel!.text! 
    MyView().parentTitle = sender.titleLabel!.text! // my attempt at assigning to variable in View Controller
}


required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
 }
}

委托将帮助您将价值传递给UIViewController

以下是您可以在 swift 中实现 delegate 的方法。

第 1 步:在 class 中声明用于 sending 数据的协议。这里是 buttonsview.

@objc protocol MyButtonDelegate{
     optional func didSelectButton(text:String)
}

第 2 步:现在在发送 class 时声明 delegate。这里是 buttonsview.

class buttonsView: UIView {
     var delegate:MyButtonDelegate?
     [other stuf......]
}

第 3 步:现在使用委托将数据发送到 'UIViewController'。

  func choseOption(sender:UIButton){

  delegate!.didSelectButton(text: sender.titleLabel!.text!)

} 

第四步:接收协议class.

 class ViewController: UIViewController,MyButtonDelegate {

第五步:在接收中实现委托方法class。

func didSelectButton(text: String) {
     parentTitle = "The Buttons title is " +  text

  }

第 6 步:现在设置 delegate

 override func viewDidLoad() {
    super.viewDidLoad()

    buttonsLeft.delegate = self


    }

希望对您有所帮助。