【问题标题】:UIView class for drop down list with UIButtons - delegate/protocol issue?带有 UIButtons 的下拉列表的 UIView 类 - 委托/协议问题?
【发布时间】:2015-06-24 23:09:11
【问题描述】:

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

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

我遇到以下问题:

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

  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 按钮视图的代码:

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

【问题讨论】:

  • 您可能想稍微分解一下这个问题。
  • 你在正确的轨道上,但不要试图将视图指向具有强引用的 ViewController。你应该看看代表团。如果您要遵循 Apple 的设计模式,您将有两个协议,一个 dataSource,具有 (NSUInteger)numberOfButtons 和 (NSString*)titleForButtonNumber:(NSUInteger)buttonIndex 之类的方法,然后是具有 (void)didSelectButtonNumber:(NSUInteger) 的单独委托协议)buttonNumber 祝你好运:)
  • @Jef 这很有意义。我不知道如何在 Swift 和苹果文档中以编程方式正确处理委派,因为它仍在 Objective C 中。但我将开始四处寻找。感谢您的关注!

标签: ios objective-c class swift delegation


【解决方案1】:

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

您可以通过以下方式在swift 中实现delegate

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


    }

希望对您有所帮助。

【讨论】:

  • 在创建buttonView 时如何将数据从UIViewController 传递到UIView 的任何建议。再次感谢!
  • 是的,我在这里解释将NSStringUIViewController 传递到UIView。在UIView 中声明NSString 就像var strTitle = String() 一样,在UIVIewController 中,您正在为buttonsView 分配内存。所以在buttonsView 对象的帮助下,您正在访问UIView 类的string 变量,例如self.buttonsLeft.strTitle = @"Hello Buttons"。您可以根据您的要求传递NSArray, NSDictionary等。
  • 如果您还有问题,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多