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

倖福魔咒の 提交于 2019-12-13 17:07:55

问题


I am trying to create a custom drop down list in a ViewController. There are going to be 5 drop down lists and each list will have 4 options. Because of the number of lists, I decided to make a UIView that has the four choices in the form of UIButtons for each of the lists. Right now I am just trying to get one down; therefore, the following code is for ONE drop down list with FIVE options (including the one selected, which I will explain further below).

Essentially what I want is to have a button showing the selected value (or a default value at launch) and then when you click on that value then the UIView that contains 4 buttons (aka the drop down list) is shown below the original button. When the user clicks on one of the buttons I want the the button with the selected value to have the title of the button that was clicked on.

I am having the following issues:

  1. I want to be able to pass the titles of the four buttons from the ViewController to the UIView because I want to use this UIView multiple times with different values for the titles of the four buttons. I don't know how to pass values to a UIView class.

  2. When a choice from the drop down list (ie a UIButton) is clicked I can't figure out how to pass the value of the title of the button from the UIView back to UIViewController. I tried setting the title to a variable in the ViewController but that didn't work (showed up as nil).

Thank you so much in advance - I know this is a long questions and I am really unsure if this is even a good approach to take for what I am trying to do but it made sense in my head.

Here is my code for the 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
    }

Here is the code for the 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")
 }
}

回答1:


Delegation will help you to pass value to UIViewController.

Here are the way you can implement delegate in swift.

Step 1 : Declare protocol in class which is used to sending data. here is buttonsview.

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

Step 2 : Now declare delegate in sending class. here is buttonsview.

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

Step 3: now use delegate to send data to 'UIViewController'.

  func choseOption(sender:UIButton){

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

} 

Step 4 : adopt protocol in receiving class.

 class ViewController: UIViewController,MyButtonDelegate {

Step 5: implement delegate method in receiving class.

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

  }

Step 6: now set delegate

 override func viewDidLoad() {
    super.viewDidLoad()

    buttonsLeft.delegate = self


    }

Hope this help you.



来源:https://stackoverflow.com/questions/29710381/uiview-class-for-drop-down-list-with-uibuttons-delegate-protocol-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!