Setting iOSSegmentedControlItem

I am trying to set the third segment in a segmented control as selected with the following code in the open event but it always highlights the first segment.
What am I missing?

[code]Me.RemoveAll

For Each icon As iOSImage In Note.Icons
IconSelector.Add(New iOSSegmentedControlItem(icon))
Next

IconSelector.Item(2).Selected = True[/code]

I’m not sure. Your code looks reasonable and this simple version in the iOSSegmentedControl Open event seems to work for me (running on iOS 13.2):

[code]Me.RemoveAll

For i As Integer = 1 To 4
Me.Add(New iOSSegmentedControlItem(i.ToText))
Next

Me.Item(2).Selected = True[/code]

The 3rd segment shows as highlighted.

[quote=498109:@Martin Fitzgibbons]I am trying to set the third segment in a segmented control as selected with the following code in the open event but it always highlights the first segment.
What am I missing?

[code]Me.RemoveAll

For Each icon As iOSImage In Note.Icons
IconSelector.Add(New iOSSegmentedControlItem(icon))
Next

IconSelector.Item(2).Selected = True[/code][/quote]

Make sure to set all other to .Selected = False
There can be only one selected, it may be the first creates one on default?

1 Like

Or try setting Selected=True before calling the Add method.

I have a similar issue in the Simulator, but not on a real device. I want to select the second item (there are only 2 items), but it never highlights in the simulator. Although on a real device it is selected just fine.

1 Like

No luck with any of those suggestions so I went back to the Notes example and tried there. I can’t get it to set any other segment other than the the first even if I tick another one in the IDE. Same behaviour in the simulator and iPad.

Is there anything we can adapt or add to IOSExtensions to make this work?

@Martin_Fitzgibbons instead of setting the iOSSegmentControlItem’s Selected property, set the parent iOSSegmentedControl’s Value property. That is the way I do it and it works.

Hi Jason, Can’t seem to get the syntax of that right.
If the segment control name is ‘Iconselector’ which has 3 segments how do I select the 2nd segment as on?

The code is setting …selected to True but nothing happens in terms of showing it as selected on the UI
self.IconSelector.Item(0).Selected = False
self.IconSelector.Item(1).Selected = True
self.IconSelector.Item(2).Selected = False

Hey Martin. To select the second segment:

IconSelector.Value = 1

There are 3 segments if I try IconSelector.Value = 2 I get out of bounds error
What’s the difference
self.IconSelector.Item(2).Selected = True & IconSelector.Value = 2

bit confused now

There seems to be a quirk with this control where the Value property defaults to -1 at design time and this causes an issue. To get around this, if I add segments in the IDE at design time, I set the Value property of the iOSSegmentedControl in the Inspector to 0.

In your case as you are adding segments at runtime, perhaps you’re experiencing a related issue after your .RemoveAll call? In any case I’ve made a little demo project for you that works for me. Let me know how you go. :slight_smile:

1 Like

That all makes sense and is what I have been trying to code into my program. I think the problem is that I am modifying the Notes example and the segments are being controlled by the overriding Note Class.
I can add a global variable to set the category and icon from a message prior to opening the Note View but it doesn’t remember it when I leave the note, which is now a different problem??

The last bit of discovery I can offer is, that it is not possible to set the selected segment in the open event but if you do it from a button (not useful) or a single timer a few ms after the view opens then it works??

I believe this is expected actually. The open event happens just before the control will be made visible so many of those properties are not yet valid (I think there is some code run after the open event to setup things you set for controls in the inspector). I would suggest you use the Xojo.Core.Timer.CallLater function to run your code after the open event

3 Likes

Is that an iOS or a Xojo thing? Either way, this is something I find deeply disturbing because there is no overview/list which properties are valid at the time of the Open event. It’s quite common I’m writing some code only to realize it’s being completely ignored.

That seems to be the case with many xojo controls in ios. It’s most likely because of auto-layout having to be done first then initalisation (open event).
It seems in xojo it’s implemented the other way around.

You could subclass the control and have the open event implement a timer call of 10ms-100ms then call an event definition you created for init.

As @Jason_King and @Jeremie_L already know. Maybe xojo could learn from it…

@Martin_Fitzgibbons a way around the issue with setting these properties in the iOSView Open event is to set them in the iOSView Constructor, which is what I do. However I don’t remove and create items at runtime as you are doing so check that this works for what you are trying to do before you go ahead and refactor it.

I would caution against overuse of Timer.CallLater. It’s handy, to be sure, but I found that I overused it and then discovered that because the methods that are referenced are internally managed within Xojo, you can’t catch exceptions if you’ve got a CallLater referencing a method that goes out of scope before it’s called. In a large/complex app this can start to get unwieldy, pretty quickly. I have found a better idea is to consider whether the event or design that you’re using might be accomplished in another way without relying on a Timer.CallLater to make it work. :slight_smile:

3 Likes