使用IBAction的Swift UIPickerView Trigger Segue

时间:2015-10-11 21:10:53

标签: ios iphone swift uipickerview ibaction

我是Swift的新手,可以使用一些关于如何在按钮点击时使用IBAction触发segue的建议。我能够使用选择器进行调整但是我希望按钮能够触发segue。我尝试在IBAction中触发函数,但这不起作用。感谢任何建议。感谢。

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    switch (row) {

    case 0: self.performSegueWithIdentifier("Segue0", sender: self)
    break;
    case 1: self.performSegueWithIdentifier("Segue1", sender: self)
    break;
    case 2: self.performSegueWithIdentifier("Segue2", sender: self)
    break;
    case 3: self.performSegueWithIdentifier("Segue3", sender: self)
    break;
    case 4: self.performSegueWithIdentifier("Segue4", sender: self)
    break;
    default: break;

    }

这是标准按钮:

@IBAction func selectButton(sender: AnyObject) {


}

2 个答案:

答案 0 :(得分:0)

我设法使用以下方法使其工作。感谢任何评论或更优雅的解决方案。 :)

添加了一个全局变量来保持选择的选择行:

var rowSelected = 0
var pickerData: [String] = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Connect data:
    self.locationPicker.delegate = self
    self.locationPicker.dataSource = self

pickerData = ["Location 1", "Location 2", "Location 3", "Location 4", "Location 5"]

// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}

// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return "\(pickerData[row])"

}
// Capture the picker view selection
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    rowSelected = row


}
@IBAction func selectButton(sender: AnyObject) {

    switch (rowSelected) {

    case 0: self.performSegueWithIdentifier("Segue0", sender: self)
    break;
    case 1: self.performSegueWithIdentifier("Segue1", sender: self)
    break;
    case 2: self.performSegueWithIdentifier("Segue2", sender: self)
    break;
    case 3: self.performSegueWithIdentifier("Segue3", sender: self)
    break;
    case 4: self.performSegueWithIdentifier("Segue4", sender: self)
    break;
    default: break;




}

答案 1 :(得分:0)

如果你要查找的是如何确定从按钮功能中选择哪一行,你可以使用UIPickerView.selectedRowInComponent(_:):

@IBOutlet weak var picker: UIPickerView! // Reference to picker from storyboard. 
@IBAction func selectButton(sender: AnyObject) {
    // Get the selected row. You only have one component, so the parameter is 0.
    let row = picker.selectedRowInComponent(0)

    // Switch block here
}

如果实际上你的segue实际命名为" Segue0"," Segue1"等等,那么你可以用以下代码替换开关块:

self.performSegueWithIdentifier("Segue\(row)", sender: self)

但是,如果你使用更多描述性的segue名称(我希望你是这样),那么你也可以将segue标识符存储在一个数组中,只需使用row作为数组的索引:

let segues = ["thisIsASegue", "thisIsAnotherSegue", "etc"]

// In the function:
self.performSegueWithIdentifier(segues[row], sender: self)