如何解决如何在动作方法中找到被点击的按钮
当我有一个对话框并且多个按钮具有相同的单击回调(操作方法)时,我通常想知道已按下了哪个按钮。我该怎么办?
示例:
class ButtonDialog : UIFrame{
void button_pressed(object self){
// how do I get this button
TagGroup pressed_button = ?;
result("The button " + pressed_button + " is pressed.\n");
}
object init(object self){
TagGroup dlg,dlg_items,button1,button2;
dlg = DLGCreateDialog("Press a button",dlg_items);
button1 = DLGCreatePushButton("Button 1","button_pressed");
button1.DLGIdentifier("button1");
dlg_items.DLGAddElement(button1);
button2 = DLGCreatePushButton("Button 2","button_pressed");
button2.DLGIdentifier("button2");
dlg_items.DLGAddElement(button2);
self.super.init(dlg);
return self;
}
}
object dialog = alloc(ButtonDialog).init();
dialog.pose();
在我当前的程序中,我从TagGroup
创建了多个行。每行都有多个按钮,但针对其特定行执行相同的操作。因此,我需要知道要修改行的按钮是哪个。 TagGroup
的长度以及行数也不是固定的。因此,我无法创建button1_pressed
,button2_pressed
,...函数,除非在运行过程中进行一些奇怪的事情与代码评估,我想尽可能避免这种情况。
解决方法
您不能在按钮的简单回调中传递参数,这实在令人不快。最简单的解决方案(尽管不是很优雅)是使用简单的单行回调方法,这些方法是唯一的,但它们自己调用通用方法,如下面的代码所示。
当然, mile7 指出在编译时按钮的数量不是固定的,这是一个问题。但是,除非(潜在的)按钮数量众多,否则这种方法仍然是最简单,最干净的方法,并且由于每个“硬编码”回叫只是一行,只是系统性的更改,因此使用Notepad ++或类似,以提供足够多的此类调用集。 (如果其中一些实际上从未使用过,也不会造成伤害。)
class ButtonDialog : UIFrame{
void button_pressed(object self,string buttonID){
// how do I get this button
TagGroup pressed_button = self.LookUpElement(buttonID);
if ( pressed_button.TagGroupIsValid() )
result("The button " + buttonID + " is pressed.\n");
else
result("The button " + buttonID + " was not found!\n");
}
// Need to be done for each button
void button_pressed_0(object self) { self.button_pressed("button0"); }
void button_pressed_1(object self) { self.button_pressed("button1"); }
object init(object self){
TagGroup dlg,dlg_items
dlg = DLGCreateDialog("Press a button",dlg_items);
number nButtons = 2
for( number n=0; n<nButtons; n++ ){
TagGroup button = DLGCreatePushButton("Button " + n,"button_pressed_" + n);
button.DLGIdentifier("button" + n);
dlg_items.DLGAddElement(button);
}
self.super.init(dlg);
return self;
}
}
object dialog = alloc(ButtonDialog).init();
dialog.pose();
,
所以我找到了可以接受的答案,但我仍然希望获得更好的结果。
我当前的想法是使用DualStateBevelButtons。如果单击按钮,状态将更改并执行回调。然后检查所有按钮的状态是否已更改。如果是这样,这是单击的按钮,并且状态被重置。
此解决方案的非常大的缺点是,只有允许带有图像的按钮,没有文本是可能的。因此,这实际上不是通用的解决方案。
rgbimage button_img = RGBImage("button-image",4,16,16);
button_img = max(0,255 - iradius / 8 * 255);
class ButtonDialog : UIFrame{
TagGroup buttons;
void button_pressed(object self){
for(number i = 0; i < buttons.TagGroupCountTags(); i++){
TagGroup button;
if(buttons.TagGroupGetIndexedTagAsTagGroup(i,button)){
if(button.DLGGetValue() == 1){
// this button is toggled so it is clicked
string identifier;
button.DLGGetIdentifier(identifier);
result("Button " + i + " (" + identifier + ") is clicked.\n");
// reset button state
button.DLGBevelButtonOn(0);
// do not continue searching,found pressed button already
break;
}
}
}
}
object init(object self){
TagGroup dlg,dlg_items,button1,button2;
dlg = DLGCreateDialog("Press a button",dlg_items);
buttons = NewTagList();
button1 = DLGCreateDualStateBevelButton("button1",button_img,"button_pressed");
buttons.TagGroupInsertTagAsTagGroup(infinity(),button1);
dlg_items.DLGAddElement(button1);
button2 = DLGCreateDualStateBevelButton("button2",button2);
dlg_items.DLGAddElement(button2);
self.super.init(dlg);
return self;
}
}
object dialog = alloc(ButtonDialog).init();
dialog.pose();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。