添加uitext字段的按钮广告子视图ios swift

时间:2016-04-24 13:01:28

标签: ios swift dropdown

我有两个按钮,即programsdata。这两个按钮有下拉选项。但是当我为program选择第一个下拉选项时,如果我从中选择任何数据我喜欢选择" super"从下拉。然后那个超级单词在我的两​​个按钮中选择。

没有选择我的第二个按钮下拉选项。它会自动更改。显然,如果我从super按钮的下拉列表中选择program数据,那个词" super"正在我的两个下拉按钮中更新。

请帮我解决这个问题:

这是我的代码:

import UIKit

class UserDetailsController: UIViewController {

    var addString: String!

      let dropDown = DropDown()




    @IBOutlet weak var programBtn: UIButton!


    @IBOutlet weak var dataBtn: UIButton!


        override func viewDidLoad() {
        super.viewDidLoad()

        dropdownmethod()


    }




    func dropdownmethod () {


        dropDown.dataSource = [
            "Car",
            "Motorcycle",
            "Van",
            "Truck",
            "Bus",
            "Bicycle",
            "Feet",
            "add"
        ]

        dropDown.selectionAction = { [unowned self] (index, item) in
            self.SpectifBtn.setTitle(item, forState: .Normal)
              self.organBtn.setTitle(item, forState: .Normal)

            if item == "add" {

                if #available(iOS 8.0, *) {
                    let alert = UIAlertController(title: "Duplicate file", message: "Afile with the same name already exists.", preferredStyle:
                        UIAlertControllerStyle.Alert)
                    alert.addTextFieldWithConfigurationHandler(self.configurationTextField)

                    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
                        print("User click Ok button")
                        print(self.textField.text)
                        self.addString = self.textField.text
                        self.dropDown.dataSource =  [   "Car",
                            "Motorcycle",
                            "Van",
                            "Truck",
                            "Bus",
                            "Bicycle",
                            "Feet", "\(self.addString)", "add"]
                        self.SpectifBtn.setTitle(self.addString, forState: .Normal)
                         self.organBtn.setTitle(self.addString, forState: .Normal)


                    }))

                    self.presentViewController(alert, animated: true, completion: {
                        print("completion block")
                    })

                } else {
                    // Fallback on earlier versions
                }
            }
        }

        dropDown.cancelAction = { [unowned self] in
            self.dropDown.selectRowAtIndex(0)
            self.SpectifBtn.setTitle("Canceled", forState: .Normal)
             self.organBtn.setTitle("Canceled", forState: .Normal)
        }

        dropDown.anchorView = SpectifBtn
        dropDown.bottomOffset = CGPoint(x: 0, y:SpectifBtn.bounds.height)
        dropDown.dismissMode = .Automatic

        dropDown.selectRowAtIndex(3)

    }




    func configurationTextField(textField: UITextField!)
    {
        if let aTextField = textField {
            textField.text = "Filename"
            self.textField = aTextField

        }
    }

    @IBAction func programBtnPressed(sender: AnyObject) {


        if dropDown.hidden {
            dropDown.show()
        } else {
            dropDown.hide()
        }
    }


    @IBAction func dataBtnPressed(sender: AnyObject) {

        if dropDown.hidden {
            dropDown.show()
        } else {
            dropDown.hide()
        }


    }





}

我的屏幕截图。当我从program下拉列表中选择任何选项时。该选项名称也会在我的data按钮名称中自动更新

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

在下拉选择某个项目之后,您应该检查它来自哪个按钮(例如通过标记),现在看来您只需在这里选择它们:

self.SpectifBtn.setTitle(item,forState:.Normal)self.organBtn.setTitle(item,forState:.Normal)