1

我正在尝试在我的 iOS Swift 应用程序中使用 ActionSheetStringPicker。我不知道如何使用 done 块或成功操作。这是我的代码:

func pickerDone()
{
    println("Here I am!")
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
    var selected = 0
    let done: ActionStringDoneBlock = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
        println(selectedIndex)
    }

    var touch = touches.anyObject() as UITouch

    if touch.view is UILabel
    {
        var picker = ActionSheetStringPicker(title: "Select an attachment", rows: attachmentsList, initialSelection: 0, doneBlock: done, cancelBlock: nil, origin: touch.view)
        picker.showActionSheetPicker()
    }
}

响应触摸的元素是一个 UILabel,称为 attachmentLabel,我想根据 ActionSheetStringPicker 中所做的选择更改它的文本。但是,当我进入 done 块时,每当我尝试访问传入的三个参数以外的任何内容时,都会收到 EXC_BAD_ACCESS 错误。如果我尝试将 pickerDone 函数用作成功操作,则仅在以下情况下调用它我将函数定义为不带任何参数。

我可以为 done 块找到的所有示例仅打印出参数的值。我需要一个示例来说明如何将选择器中的信息返回到我的 UI。

我试图弄清楚如何捕获变量以在我的闭包中使用,但我得到了相同的 EXC_BAD_ACCESS 错误。我尝试捕获本地,但没有奏效:

var selected = 0
let done: ActionStringDoneBlock = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
    selected = selectedIndex
}

所以然后我尝试捕获自我,但这也不起作用:

let done: ActionStringDoneBlock = {[weak self](picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
    self?.attachmentLabel.text = self?.attachmentsList[selectedIndex]
    return
}
4

1 回答 1

0

我是 ActionSheetPicker 的所有者。我没有尝试使用 touchesBegan 覆盖,但它应该是一样的。

我尝试将您的代码从 to 包装起来touchesBegan@IBAction它可以工作(单击完成后打印索引):

@IBAction func doneBlockInvokeByDoneClicked(sender: UIButton) {
        var selected = 0
        let done: ActionStringDoneBlock = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
            println(selectedIndex)
        }

        var picker = ActionSheetStringPicker(title: "Select an attachment", rows: ["One", "Two", "A lot"], initialSelection: 0, doneBlock: done, cancelBlock: nil, origin: sender.superview!.superview)

        picker.showActionSheetPicker()

    }

这是从 done 块运行函数的示例:

func pickerDone()
{
    println("Here I am!")
}

@IBAction func functionInvokeByDoneCicked(sender: UIButton) {
    var picker = ActionSheetStringPicker(title: "title", rows: ["1","2"], initialSelection: 0, target: self, successAction: "pickerDone", cancelAction: nil, origin: sender.superview!.superview)

    picker.showActionSheetPicker()
}

您的 origin 参数可能有问题。检查这touch.view是主视图。

于 2015-01-23T20:35:40.617 回答