链接两个选择器与一个按钮,一个结果迅速

时间:2017-04-13 17:20:19

标签: swift uipickerview

我希望用户这样做:当他们从第一个选择器和另一个选择器上的项目中选择一个项目并单击该按钮时,他们会在文本字段中获得结果。我怎么能这样做?

我已经创建了两个选择器,文本字段和一个按钮。例如:如果用户从第一个微调器中选择他们的名字,而第二个微调器就是他们的id,当他们点击按钮时,文本视图将显示他们的电话号码。

这是我到目前为止所做的:

// //  ViewController.swift //  test001 // //  Created by Ahmed  N. on
7/14/1438 AH. //  Copyright © 1438 Ahmed  N. All rights reserved. //

import UIKit

class Viewkm: UIViewController,
UIPickerViewDataSource,UIPickerViewDelegate {
    @IBOutlet weak var array1: UIPickerView!
    @IBOutlet weak var array2: UIPickerView!
    @IBOutlet weak var result: UITextField!

    var array:NSArray = []
    var array12:NSArray = []

    override func viewDidLoad() {
        super.viewDidLoad()
        array = ["3","2","1"]
        array12 = ["Nasser","meachal","Ahmed"]
        // Do any additional setup after loading the view, typically from a nib.
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if (pickerView.tag == 1){
            return array.count
        }else{
            return array12.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if (pickerView.tag == 1){
            return "\(array[row])"
        }else{
            return "\(array12[row])"
        }
    }

    public func numberOfComponents(in pickerView: UIPickerView) -> Int{
        return 1
    }

    @IBAction func getResult(_ sender: UIButton) {
        var select1 = array[array1.selectedRow(inComponent: 0)]
        var select2 = array12[array2.selectedRow(inComponent: 0)]

        if (select1 == "1") && (select2 = "Ahmed")  {
            getResult.setTitle("Telephone number...", result: .Normal)
        }
    }
}

****更新****

//    //  ViewController.swift    //  test001    //    //  Created       by Ahmed  N. on 7/14/1438 AH.    //  Copyright © 1438 Ahmed  N. All
     

版权所有。 //

     

导入UIKit

     

// +++++++++++++++++++++++++++++++++++++++++++++++++++   的UIViewController,
  UIPickerViewDataSource,UIPickerViewDelegate {

@IBOutlet weak var array1: UIPickerView!
@IBOutlet weak var array2: UIPickerView!
//this is a text failt that i want to set tge resut in
@IBOutlet weak var result: UITextField!

// i did these two 

var selectedRow1 : Int
var selectedRow2 : Int

var array:NSArray = []
var array12:NSArray = []

// array fo numbers
var arrayNumbers:NSArray = []


override func viewDidLoad() {
    super.viewDidLoad()
    array = ["3","2","1"]
    array12 = ["Nasser","meachal","Ahmed"]
    arrayNumbers = ["400","500","4000"]






}



func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,   inComponent component: Int) {
    if (pickerView.tag == 1){
        self.selectedRow1 = row
    }else{
        self.selectedRow1 = row
    }
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if (pickerView.tag == 1){
        return array.count
    }else{
        return array12.count
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if (pickerView.tag == 1){
        return "\(array[row])"
    }else{
        return "\(array12[row])"
    }


}
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
    return 1
}




@IBAction func getResult(_ sender: UIButton) {
    var select1 = array[self.selectedRow1]
    var select2 = array12[self.selectedRow2]


    // error syaed : binert opreater "==" cannot be applied to type "any" or "String"
    if (select1 == "1") && (select2 = "Ahmed")  {
        //And you can show the number from array list

        // ******************** error sayed : cannot call value of non-function type String
        result.text("Telephone number...\(self.arrayNumber[self.selectedrow1])", result: .Normal)
    }
}



}

1 个答案:

答案 0 :(得分:1)

您可以使用pickerview委托方法。

  //those two for selected pickerView rows
    var selectedRow1 : Int
    var selectedRow2 : Int
    //this array for the numbers. You should fill this array with users number maybe.
    var arrayNumbers:NSArray = []
    //pickerView delegate method, the row keyword shows which row is selected
    optional func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,   inComponent component: Int) { 
    if (pickerView.tag == 1){
                self.selectedRow1 = row
            }else{
                self.selectedRow1 = row
            }
    }
    //your button action be like this
    @IBAction func getResult(_ sender: UIButton) {
            var select1 = array[self.selectedRow1]
            var select2 = array12[self.selectedRow2]
            //your arrray type is Any
            if (String(select1) == "1") && (String(select2) = "Ahmed")  {
    //And you can show the number from array list
                getResult.setTitle("Telephone number...\(self.arrayNumber[self.selectedrow1])", result: .Normal)
            }
    }