iOSView sharing data between

Hi There,

Just learning the new framework and iOS coding in Xojo, and I’m having problems controlling one view from another.

View1 is the default view. The user pushes a button.

dim details As new View2 self.pushTo(details)

View2 slides in.

I then want View2 to change something in View1.

View1.tableData.removeBase(row) View1.Table1.ReloadData

However the compiler returns an error.

[quote]This item does not exist
View1.saveData.removeBase[/quote]

It returns the same error for both lines, I know “View1.saveData” exists as I used tab autocomplete to type it.

Are 2 views not allowed to work this way, or am I (probably) doing it wrong?

Thanks,
Stephen

You cannot read properties and methods from another View. You have to pass properties when creating the new View or use global properties and methods.

Your best bet is a module where you save your properties for such so all views can read/write them.

No need to shadow all properties. There is another way that allows to keep views around and access their properties at any time from wherever :

  • Create a global dictionary in a module. I called it Dico
  • In app, put
Dico = New Dictionary
  • In View 1 Deactivate, so the view is saved before pushing another one :

dico.Value("View1") = self

I added a label to View1
I then added a button to pushto a View 2 that contains two buttons :

Sub Action() View1(Dico.Value("View1")).Label1.Text = "Pickaboo" End Sub

Sub Action() self.close End Sub

So one button modifies the content of Label1.Text on View1 from View2, the second one closes View2.

When View1 reappears, the label has been modified.

Note that with this technique you no longer need to create a new view when you want to display again a view that already exists. For instance :

if Dico.HasKey("View2") then PushTo(View2(Dico.Value("View2"))) else Dim newView As New View2 Self.PushTo(newView) end if

Sounds like a job for Class Interfaces?

Just ran a quick test and I can get two views talking to each other, that is passing data back and forth, using a Class Interface…

It seems as though the OP is wanting to refresh data in View1 when something is changed in View2. The approach that I use for this scenario is to define a method in View1 to handle a callback and a delegate in View2 that has matching parameters (representing anything that I need to pass to the callback method) and a local property in View2 with the delegate as its type. Then, in the constructor for View2, I have a property of the delegate type that I assign to a local View2 property. So then when I create an instance of View2 from View1, I pass in the WeakAddressOf the View1 callback method and I invoke the callback method as required from View2.

So my View2 Constructor method would look something like this:

// Calling the overridden superclass constructor.
Super.Constructor
  
'Remember the callback
CallBack = CallBackMethod

And I would call View2 from View1 like this:

dim newView as new View2(WeakAddressOf CallBackMethod)
PushTo(newView)

When I need to callback to View1 to update something, I do something like this in View2:

if CallBack <> nil then
   CallBack.Invoke
end