UIView类下拉列表与UIButtons - 委托/协议问题? [英] UIView class for drop down list with UIButtons - delegate/protocol issue?

查看:135
本文介绍了UIView类下拉列表与UIButtons - 委托/协议问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ViewController中创建一个自定义下拉列表。有5个下拉列表,每个列表将有4个选项。因为列表的数量,我决定做一个UIView有四个选择在UIButtons的形式为每个列表。现在我只是想得到一个;因此,以下代码是针对ONE下拉列表与FIVE选项(包括选择的一个,我将在下面进一步解释)。



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



我遇到以下问题:


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


  2. 当点击下拉列表中的一个选项(即UIButton) t计算如何将按钮的标题的值从UIView传递回UIViewController。我试图将标题设置为ViewController中的一个变量,但是没有工作(显示为nil)。


非常感谢你 - 我知道这是一个很长的问题,我真的不确定这是否



这里是我的代码为ViewController


。这是一个很好的方法来采取我想要做的,
var buttonsLeft:buttonsView = buttonsView()//这是UIView子类

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

override func viewWillAppear(animated:Bool){

//隐藏下拉列表
self.buttonsLeft.frame = CGRect(x:self.view。 boundswidth *(1/6) - 50,y:120,width:100,height:135)
self.buttonsLeft.hidden = true

//在启动时的默认值
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)
}
//此函数显示列表
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框架)
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。正常)

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:chosenOption:,forControlEvents:UIControlEvents.TouchUpInside)
buttons [i] .hidden = false
self.addSubview(buttons [i])
}

}


func chosenOption(sender:UIButton){
self.title = sender.titleLabel!.text!
MyView()。parentTitle = sender.titleLabel!.text! //我试图分配给视图控制器中的变量
}


需要init(coder aDecoder:NSCoder){
fatalError(init未实施)
}
}


解决方案

委派将帮助您将值传递给 UIViewController



以下是您可以在 swift中实现委托的方式



步骤1:在 class 中声明协议,用于 code>数据。这里是 buttonsview

  @objc protocol MyButtonDelegate {
可选的func didSelectButton(text:String)
}

步骤2: $ c> delegate 。这里是 buttonsview

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

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

  func chosenOption(sender:UIButton){

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

}

步骤4:在接收类中采用协议。

  class ViewController:UIViewController,MyButtonDelegate {

步骤5:在接收类中实现委托方法。

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

}

步骤6:现在设置委托

  override func viewDidLoad(){
super.viewDidLoad()

buttonsLeft.delegate = self


}

希望这有助于你。


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")
 }
}

解决方案

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.

这篇关于UIView类下拉列表与UIButtons - 委托/协议问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆