UIView类下拉列表与UIButtons – 委托/协议的问题?

我想在ViewController中创build一个自定义的下拉列表。 将会有5个下拉列表,每个列表将有4个选项。 由于列表的数量,我决定做一个UIView,每个列表都有UIButtonsforms的四个选项。 现在我只是想让一个人失望。 因此,下面的代码是一个下拉列表与五个选项(包括select的一个,我将在下面进一步解释)。

基本上我想要的是有一个button显示选定的值(或启动时的默认值),然后当你点击该值,那么包含4个button(又名下拉列表)的UIView显示在原始button的下方。 当用户点击其中一个button时,我想让具有所选值的button具有被点击的button的标题。

我有以下问题:

  1. 我想能够从ViewController传递到UIView的四个button的标题,因为我想多次使用这个UIView不同的值为四个button的标题。 我不知道如何将值传递给UIView类。

  2. 当从下拉列表中select(即UIButton),我不知道如何将button的标题的值从UIView传回给UIViewController。 我尝试在ViewController中设置标题为一个variables,但没有工作(显示为零)。

非常感谢你 – 我知道这是一个很长的问题,我真的不确定这是否是一个很好的方法去接受我正在做的事情,但是在我的脑海里是有道理的。

这是我的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:在用于sending数据的class中声明协议。 这里是buttonsview

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

步骤2:现在在发送类中声明delegate 。 这里是buttonsview

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

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

  func choseOption(sender:UIButton){ delegate!.didSelectButton(text: sender.titleLabel!.text!) } 

第四步:在接收类中采用协议。

  class ViewController: UIViewController,MyButtonDelegate { 

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

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

第6步:现在设置delegate

  override func viewDidLoad() { super.viewDidLoad() buttonsLeft.delegate = self } 

希望这对你有所帮助。