在Livecode中,我试图让按钮与"连接" /"断开"功能。如何让按钮知道它之前是否已经被点击过,执行代码A然后再次点击时应该执行代码B?需要在"连接"之间多次点击该按钮。和"断开"
答案 0 :(得分:1)
如果您使用标准按钮并且只有2个状态(断开连接/已连接),则一种简单的方法是禁用按钮的autoHilite属性并在其脚本中手动设置按钮的hilite:
on mouseUp
set the hilite of me to not the hilite of me
if the hilite of me then
-- do connecting stuff here
else
-- do disconnecting stuff here
end if
end mouseUp
您没有解释为什么需要多次单击按钮,但假设您需要两个以上的连接状态,则可以使用自定义属性来存储当前状态。例如,您可以为每个州使用“空”(断开连接),“握手”(启动流程),“连接”(正在处理)和“链接”(已连接)的值。类似的东西:
# STORE CURRENT CONNECTING STATE IN connectionState
on mouseUp
switch the connectionState of me
case empty
-- start connection process, show HANDSHAKING feedback here
hilite me
set the connectionState of me to "handshaking"
break
case "handshaking"
-- if initial handshake successful, begin connecting to system
if handShakeSuccessful is true then
-- start connection process, show CONNECTING feedback here
set the connectionState of me to "connecting"
end if
break
case "connecting"
-- if initial connection is successful, show LINKED feedback here
if connectionSuccessful is true then
set the connectionState of me to "linked"
end if
break
case "linked"
-- do disconnecting stuff here
set the connectionState of me to empty
unhilite me
end switch
end mouseUp
答案 1 :(得分:0)
在ChatRev堆栈中,我们使用按钮的标签来指示其状态。
if the label of me is "Connect" then
set the label of me to "Disconnect"
open socket gSocket
write "helo" & cr to socket gSocket
read from socket gSocket with message "connected"
else // "Disconnect"
set the label of me to "Connect"
write "disconnect" to socket gSocket
repeat for each line myLine in the openSockets
close socket myLine
end repeat
end if
如果标签是"已连接"连接应该关闭,如果标签是" Connect"应该建立联系。
答案 2 :(得分:0)
我通常在这种情况下使用setProp处理程序。该按钮仅用作“切换”开关。
# the button script
on mouseUp
set the uConnectedState of this cd to not the uConnectedState of this cd
end mouseUp
# in the card script (or wherever appropriate)
setProp uConnectedState pIsConnected
if pIsConnected then
set the label of btn "connect" to "Disconnect"
# any other interface updates or logic here
else
set the label of btn "connect" to "Connect"
# any other interface updates or logic here
end if
pass uConnectedState
end uConnectedState
这样做意味着我所要做的就是设置自定义属性,所有业务都由setProp处理程序处理。例如,如果我需要在openCard上初始化连接,我所要做的就是将卡的uConnectedState设置为false / true。