Widget.newSegmentedControl resetting on error

I need that this widget when i press to one segments make a control and in case fail that control go back to the default segment automatcly.

How i can reset the segment control? i show you the listener that i’m using:

if db_fn.ExistMoon(target.segmentLabel) then

moonselected = target.segmentLabel

else

native.showAlert( “Error!”, “”, { “OK” } )

– SOMETHING TO RESET THE SEGMENT CONTROL –

end

It looks to me like a function to set the segment was added in a daily build after the current public build.  Pro and Enterprise subscribers get early access to new features.  So for now, using the latest public build, the answer is no, there isn’t a way.

Rob

There is actually a way. You can try the following. This is an under the hood and unsupported way of getting segmented control to do what you need and will possibly not work forever but hopefully you will gain access to the new proper method once the next public release goes live and therefore not need this forever… Enjoy. 

 local segments = { "The", "Corona", "SDK", "Widget", "Demo!" } -- Create a default segmented control (using widget.setTheme) local segmentedControl = widget.newSegmentedControl { left = 10, top = 60, segments = segments, defaultSegment = 3, onPress = segmentedControlListener, } local segmentNumToSetTo = 1 -- Change this number to which segment you wish to activate if 1 == segmentNumToSetTo then segmentedControl:setLeftSegmentActive() elseif #segments == segmentNumToSetTo then segmentedControl:setRightSegmentActive() else segmentedControl:setMiddleSegmentActive( segmentNumToSetTo ) end segmentedControl.\_segmentNumber = segmentNumToSetTo;

Thank for yours answer. Waitning for next release i arrange a solution like that:

if db\_fn.ExistMoon(target.segmentLabel) then moonselected = tonumber(target.segmentLabel) lastsegment = tonumber(target.segmentLabel) else native.showAlert( "Error!", "", { "OK" } ) segmentedControl:removeSelf( ) segmentedControl = widget.newSegmentedControl { x = display.contentWidth \* 0.75, y = display.contentHeight \* 0.25, segments = { "1", "2", "3" }, defaultSegment = lastsegment, onPress = segmentedControlListener, } end

Probably someone can use this solution too :slight_smile:

It looks to me like a function to set the segment was added in a daily build after the current public build.  Pro and Enterprise subscribers get early access to new features.  So for now, using the latest public build, the answer is no, there isn’t a way.

Rob

There is actually a way. You can try the following. This is an under the hood and unsupported way of getting segmented control to do what you need and will possibly not work forever but hopefully you will gain access to the new proper method once the next public release goes live and therefore not need this forever… Enjoy. 

 local segments = { "The", "Corona", "SDK", "Widget", "Demo!" } -- Create a default segmented control (using widget.setTheme) local segmentedControl = widget.newSegmentedControl { left = 10, top = 60, segments = segments, defaultSegment = 3, onPress = segmentedControlListener, } local segmentNumToSetTo = 1 -- Change this number to which segment you wish to activate if 1 == segmentNumToSetTo then segmentedControl:setLeftSegmentActive() elseif #segments == segmentNumToSetTo then segmentedControl:setRightSegmentActive() else segmentedControl:setMiddleSegmentActive( segmentNumToSetTo ) end segmentedControl.\_segmentNumber = segmentNumToSetTo;

Thank for yours answer. Waitning for next release i arrange a solution like that:

if db\_fn.ExistMoon(target.segmentLabel) then moonselected = tonumber(target.segmentLabel) lastsegment = tonumber(target.segmentLabel) else native.showAlert( "Error!", "", { "OK" } ) segmentedControl:removeSelf( ) segmentedControl = widget.newSegmentedControl { x = display.contentWidth \* 0.75, y = display.contentHeight \* 0.25, segments = { "1", "2", "3" }, defaultSegment = lastsegment, onPress = segmentedControlListener, } end

Probably someone can use this solution too :slight_smile: