[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?
For Each icon As iOSImage In Note.Icons
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.
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.
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
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.
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
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.
@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.