我正在尝试在iOS Swift应用中使用ActionSheetStringPicker。我无法弄清楚如何使用完成块或成功操作。这是我的代码:
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中的选择更改文本。但是,当我进入完成块时,每当我尝试访问传入的三个参数以外的任何内容时,我都会收到EXC_BAD_ACCESS错误。如果我尝试使用pickerDone函数作为成功操作,则仅在调用时调用我定义函数不带任何参数。
我可以为完成块找到的所有示例仅打印出参数的值。我需要一个示例,说明如何将选择器中的信息返回到我的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
}
答案 0 :(得分:0)
我是ActionSheetPicker的所有者。我没有尝试使用touchesBegan overriding,但它应该是相同的。
我尝试将代码从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()
}
以下是从完成块运行函数的示例:
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
是否为主视图。