Problem with native alert in newButton

Hi,

I am having difficulties seeing where my problem is located, probably obvious but its causing me to pull whats left of my hair out.

I have created a standard widget.newButton which is designed to clear settings in my app. For what I think is good practice I want to check that they intend to wipe settings and thus I called a native.alert.

The alert shows but the listener doesn’t seem to receive the clicked action.

Ive checked scope and its not that, but clearly the call to the alert is inside the listener for the widget button.

Any ideas?

What version of Corona SDK are you using?

Can you post some code?

Thanks

Rob

Hi Rob,

i was on version 2013.1202, just downloading latest, which seems to have moved on, perhaps a bug got fixed?

Anway, here is the relevant code in the button listener and then the alert listener.

 function onClear_Button (event)

    local phase = event.phase

    local target = event.target

    local clear_memory = false

    if (“began” == phase) then

        local alert = native.showAlert( “Clear Memory”, “Are you sure?” ,{ “OK”,“Cancel”}, onComplete ) 

        if alert then

            program_counter=0

               instruction_register = 0

               accumulator = 0

               memory_address_register = 0

               for i = 0,99 do

                   memory[i] = 0

               end

               show_pc()

               show_acc()

               show_ir()

               opcode =""

               show_opcode()

               show_mar()

               show_memory()

               clear_io_unit()

               show_io_unit()

               stage_message = “Clear button pressed”

               detail_message = “Program cleared and values reset”

               show_messages()

           end

    end

    return true 

end

function onComplete(event)

    if “clicked” == event.action then

        if “1” == event.index then

            return true 

        elseif “2” == event.index then

            return false

        end

    end

end

This is my first app with the sdk and generally it is a pleasant experience. Wish there were a few more UI functions, and clearer file handling. Im spending much of my time recreating menus from atoms and also writing some higher level file operations from lfs.

thanks anyway for all the good work.

Martin

I would move all of this:

            program\_counter=0                instruction\_register = 0                accumulator = 0                memory\_address\_register = 0                for i = 0,99 do                    memory[i] = 0                end                show\_pc()                show\_acc()                show\_ir()                opcode =""                show\_opcode()                show\_mar()                show\_memory()                clear\_io\_unit()                show\_io\_unit()                stage\_message = "Clear button pressed"                detail\_message = "Program cleared and values reset"                show\_messages()

Grrr.   Lost the rest of my post…

Move it here:

function onComplete(event)     if "clicked" == event.action then         if "1" == event.index then             --             -- PUT THE CODE HERE             --             return true          elseif "2" == event.index then             return false         end     end end

The native.showAlert() is an asynchronous function.  That is, it created the dialog box and immediately returns to your code and continues executing the code below it   It does not stop and wait on an interaction.  If you read the docs for the function you will see that it returns not a Yes or no if it was clicked (it doesn’t know that yet), but it returns and object ID for the alert so you could cancel it with software later if need be. 

The result of this is the object ID will always test as “true” in your line:   if alert then…  Thus right now your code shows the dialog box and then resets your settings.

Since your alert callback function doesn’t do anything, the user never really has control on resetting the data.  Hope that helps you understand what’s going on.

Rob

Hi againRob, I knew about the asynchronous and originally I had my code where you say. If I put a print at the start of the onComplete before the if clicked then it never gets there. I’ve just downloaded latest build and will be “happily” redoing my coordinates for display objects. Interesting that widgets stay top left.

Here’s the altered version as you suggested, as before the dialog appears, i click the OK button and the code isn’t run. Print statements inside and outside the if statement do not get executed either.

local function onClear_Button (event)

    local phase = event.phase

    local target = event.target

    local clear_memory = false

    if (“began” == phase) then

        local alert = native.showAlert( “Clear Memory”, “Are you sure?” ,{ “OK”,“Cancel”}, onComplete ) 

    end

    return true 

end

function onComplete(event)

    if “clicked” == event.action then

        if “1” == event.index then

            program_counter=0

               instruction_register = 0

               accumulator = 0

               memory_address_register = 0

               for i = 0,99 do

                   memory[i] = 0

               end

               show_pc()

               show_acc()

               show_ir()

               opcode =""

               show_opcode()

               show_mar()

               show_memory()

               clear_io_unit()

               stage_message = “Clear button pressed”

               detail_message = “Program cleared and values reset”

               show_messages()

            return true 

        elseif “2” == event.index then

            return false

        end

    end

end

Any ideas?

This:

if “1” == event.index then

should be:

if 1 == event.index then

event.index is a number not a string.

Rob

Duh!!! Too much coffee… or too little?

thanks! another problem solved.

Oops, spoke too soon.

In the simulator it does indeed work now, but in the device itself it carries out the action then wherever I click on the screen from then on it repeats the alert. I suspect it is to do with focus, but cannot see where as the “clicked” action is apparently when the alert has been dismissed. Do I need to purge the native alert?

Martin

What version of Corona SDK are you using?

Can you post some code?

Thanks

Rob

Hi Rob,

i was on version 2013.1202, just downloading latest, which seems to have moved on, perhaps a bug got fixed?

Anway, here is the relevant code in the button listener and then the alert listener.

 function onClear_Button (event)

    local phase = event.phase

    local target = event.target

    local clear_memory = false

    if (“began” == phase) then

        local alert = native.showAlert( “Clear Memory”, “Are you sure?” ,{ “OK”,“Cancel”}, onComplete ) 

        if alert then

            program_counter=0

               instruction_register = 0

               accumulator = 0

               memory_address_register = 0

               for i = 0,99 do

                   memory[i] = 0

               end

               show_pc()

               show_acc()

               show_ir()

               opcode =""

               show_opcode()

               show_mar()

               show_memory()

               clear_io_unit()

               show_io_unit()

               stage_message = “Clear button pressed”

               detail_message = “Program cleared and values reset”

               show_messages()

           end

    end

    return true 

end

function onComplete(event)

    if “clicked” == event.action then

        if “1” == event.index then

            return true 

        elseif “2” == event.index then

            return false

        end

    end

end

This is my first app with the sdk and generally it is a pleasant experience. Wish there were a few more UI functions, and clearer file handling. Im spending much of my time recreating menus from atoms and also writing some higher level file operations from lfs.

thanks anyway for all the good work.

Martin

I would move all of this:

            program\_counter=0                instruction\_register = 0                accumulator = 0                memory\_address\_register = 0                for i = 0,99 do                    memory[i] = 0                end                show\_pc()                show\_acc()                show\_ir()                opcode =""                show\_opcode()                show\_mar()                show\_memory()                clear\_io\_unit()                show\_io\_unit()                stage\_message = "Clear button pressed"                detail\_message = "Program cleared and values reset"                show\_messages()

Grrr.   Lost the rest of my post…

Move it here:

function onComplete(event)     if "clicked" == event.action then         if "1" == event.index then             --             -- PUT THE CODE HERE             --             return true          elseif "2" == event.index then             return false         end     end end

The native.showAlert() is an asynchronous function.  That is, it created the dialog box and immediately returns to your code and continues executing the code below it   It does not stop and wait on an interaction.  If you read the docs for the function you will see that it returns not a Yes or no if it was clicked (it doesn’t know that yet), but it returns and object ID for the alert so you could cancel it with software later if need be. 

The result of this is the object ID will always test as “true” in your line:   if alert then…  Thus right now your code shows the dialog box and then resets your settings.

Since your alert callback function doesn’t do anything, the user never really has control on resetting the data.  Hope that helps you understand what’s going on.

Rob

Hi againRob, I knew about the asynchronous and originally I had my code where you say. If I put a print at the start of the onComplete before the if clicked then it never gets there. I’ve just downloaded latest build and will be “happily” redoing my coordinates for display objects. Interesting that widgets stay top left.

Here’s the altered version as you suggested, as before the dialog appears, i click the OK button and the code isn’t run. Print statements inside and outside the if statement do not get executed either.

local function onClear_Button (event)

    local phase = event.phase

    local target = event.target

    local clear_memory = false

    if (“began” == phase) then

        local alert = native.showAlert( “Clear Memory”, “Are you sure?” ,{ “OK”,“Cancel”}, onComplete ) 

    end

    return true 

end

function onComplete(event)

    if “clicked” == event.action then

        if “1” == event.index then

            program_counter=0

               instruction_register = 0

               accumulator = 0

               memory_address_register = 0

               for i = 0,99 do

                   memory[i] = 0

               end

               show_pc()

               show_acc()

               show_ir()

               opcode =""

               show_opcode()

               show_mar()

               show_memory()

               clear_io_unit()

               stage_message = “Clear button pressed”

               detail_message = “Program cleared and values reset”

               show_messages()

            return true 

        elseif “2” == event.index then

            return false

        end

    end

end

Any ideas?

This:

if “1” == event.index then

should be:

if 1 == event.index then

event.index is a number not a string.

Rob

Duh!!! Too much coffee… or too little?

thanks! another problem solved.

Oops, spoke too soon.

In the simulator it does indeed work now, but in the device itself it carries out the action then wherever I click on the screen from then on it repeats the alert. I suspect it is to do with focus, but cannot see where as the “clicked” action is apparently when the alert has been dismissed. Do I need to purge the native alert?

Martin