iOS Best Practice for Passing in / Retrieving Individual View Properties

In my app I have master (table) Views and detail (edit) Views. I’ve followed the examples in XojoNotes and Eddies’ Electronics for referencing the calling class as a property and passing that between Views to obtain the results of the edit. That all works well.

However I sometimes need to pass a single value to a View and have that value returned. An example is for editing a date within a table. Because Xojo doesn’t yet support custom table cells, I have a DateTimeView that contains a UIDatePicker which I want to PushTo whenever the user needs to select a date. I don’t know what class might need to set the date (I have about 20) so I can’t have a specific class as a property of this date selection View. In other words, I want it to be a generic date selection View. Ideally I’d like to have a single Date property that is passed in and returned. I’ve got it working such that I can set the Date property of the View when I create it, but I don’t know how to obtain the property back because the View runs asynchronously and the reference to it is lost as soon as the calling method finishes.

Is there a “best practice” way to deal with this?

You could use an Interface to create a generic way to save a date value to any class/view. I’m slinging code here in the forum editor, so it is likely not 100% correct, but this is the general idea:

Interface DateSaver Sub SetDate(d As Xojo.Core.Date)
In your DateTimeView, add a property:

ParentDateSaver As DateSaver

In DateTimeView, you can save the date with this:

If ParentDateSaver <> Nil Then ParentDateSaver.SetDate(dateValueToSave) End If

Now add the DateSaver interface to classes/Views where you need the date that was set by DateTimeView. This should automatically add the SetDate method for you to implement. You should add code to save the date that is passed to a property that you can then use how you want.

Sub SetDate(d As Xojo.Core.Date) MyDateProperty = d End Sub

When you call DateTimeView, you’ll need to set its ParentDateSaver property:

Dim v As New DateTimeView v.ParentDateSaver = Self Self.PushTo(v)

You can now check and use MyDateProperty as necessary.

I’m not sure there is a best practice yet. But there are several ways to accomplish this.

You can do what Paul did. Or you could add an event to the client view and use AddHandler in the Parent view. That way the client view doesn’t really need to know about the parent. It raises an event and the parent either handles it or not.

Bob’s suggestion is pretty slick. You’d have to add an Event Definition to DateTimeView:

Event DateSet(d As Xojo.Core.Date)

Somewhere on DateTimeView, you’d call DateSet with the date:


Then in a view that displays DateTimeView, you can use AddHandler like this:

Dim v As New View2 AddHandler v.DateSet, AddressOf DateSetHandler Self.PushTo(v)

You’ll need to create a method in the view called DateSetHandler with this signature:

Sub DateSetHandler(v As DateTimeView, d As Xojo.Core.Date) Label1.Text = d.ToText RemoveHandler v.DateSet, AddressOf DateSetHandler End Sub

Thanks Bob. Thats a very good ‘trick’. :slight_smile: Never thought about that.

Not really a trick. Since we use a lot of Container controls (which iOS doesn’t have yet) in desktop and web it is very handy to use. That way your dynamic controls, children whatever’s, don’t need to know anything about the parent. They fire an event and either the parent handles it or not.

Passing in the parent to the child is perfectly legit. I did it for years. But as soon as you have to use it in more than one place you quickly start to realize the problem plus it just doesn’t seem very OO. Events and AddHandler to the rescue!

And that’s why I love this forum. Thanks team! :slight_smile: