Send data from modal screen (dialog) to another screen

Title has it. How do I send data from a modal screen (dialog) to another screen? Or from one screen to another? Or set a string to open when launching a second screen?

With delegates.

need more help with delegates.

I’ve thought of writing to a special folder (resource or documents). Doesn’t seem to work in the virtual iOS device. Perhaps on a real device.

Don’t do that. It’s a huge waste of resources.

If all you’re doing is sending data from a parent screen to a detail screen, you just need to put a property in the detail screen to hold the item and use it in the opening event to set up things the way you need to. Then when creating that screen later you do something like this:

Var detail as new DetailScreen
Detail.propertyName = dataToTransfer
Self.pushTo(detail)

If however you need a two-way communication, delegates are certainly one way to go. Basically you can create a delegate on a class and then pass a pointer to a method that matches the delegate signature when the class needs to send data, it can check to see if the delegate property has been set and if so, call the corresponding method. xojo has examples on how delegates work.

Here is a Xojo example using delegates:

2 Likes

thanks, will try that.

Wow, looks simple. Still don’t understand declares, but this doesn’t look complicated. Will use it, then look carefully to try to understand.

Declares and Delegates are two completely different things. :wink:

1 Like

Can the ‘self.pushto()’ function address the original screen, not a new screen? When I use this repeatedly get a stack of screens (alternating parent and modal). Not great.

Besides Greg’s example, you can also communicate with existing screens to execute a method or change a property.

MasterScreen(Self.ParentSplitView.Master).ContinueButtonState(True)

That’s what my example is doing. Self is the current screen, detail is the new screen.

But it seems that I gave you the deprecated method. The new one would be:

Detail.Show(Self)

Perhaps I’m not understanding. When I replace self.push() with ‘detail.show’, I still get stacked screens. The problem is that detail is a newly created screen, not the original parent. Need a way to reference the parent, which isn’t obvious.

Also tried to implement the declare method; this fails since, for some reason, the constructor for the modal screen doesn’t fire. (the example works perfectly; but when I insert the code into my screens, which are fairly complex, the constructor doesn’t fire).

(the 'parent.splitview function doesn’t work since the parent isn’t a split view; just a parent).

thanks for the help, but remain frustrated

Perhaps I’m not. Could you show a screenshot of what is wrong? FWIW, modal views do appear on top of others on iOS.

It’s very important that we get the terminology corrected here because using the word “declare” means something completely different. What we’re talking about is a delegate.

Then use another method or event. Opening should work or just make a method that you call when you create it.

yes, showmodal produces a model screen on top of the calling screen. The issue is having the modal change something in the calling screen. (screens are embarassing).

The lines I get to work a bit always start with “Var detail as new DetailScreen” which creates a new detail screen and doesn’t address the parent. Also results in stacked screens, which I don’t want.

my bad. I meant delegate, not declare. I was pretty careful in the implementation (not in writing the post above).

I’ll try making a new method. Not very clear about the scope of what constructors do.

thanks for your help.

(by the way, the app works pretty well in single screen mode. but a modal screen would make what’s going on much clearer to users; the app is a port of a desktop and web app that is a two-way atlas in neuroanatomy. Current images looks something like this).

The fact that the constructors don’t fire is a bug, but not something you can work around.

There’s a trick we can use to make this all a little easier since objects are always passed by reference.

  1. Make a property on the detail screen whose type is MobileScreen, named Parent.

  2. Add a new method to the detail screen: ShowModal( parent As MobileScreen, style As ModalPresentationStyles = ModalPresentationStyles.Automatic, animate As Boolean = True)

With the code:

Self.ParentScreen = Parent
Super.ShowModal(parent, style, animate)

Now, in your detailscreen, when you need to do something on the main screen, you can just do

MainScreen(parent).methodName

Now I’m going to suggest that you make special methods just for this purpose. Don’t modify controls and properties directly.

Also, in any place where you close the dialog, you should set parent = Nil.

1 Like

sounds good. tomorrow.

thanks again.

Not working.

I’ve added a line (line 2, below, in asterisks) from the parent screen.
var adetail as new detail
adetail.parent=self
adetail.ShowModal

Now parent in detail screen is a reference to the main screen. (when I pause, parent is a ref to parent).

the code you suggested:

MainScreen(parent).methodName

doesn’t work. After the period, if I enter a method in the parentscreen, it says it doesn’t exist. Tab after the periodgives native functions, like ‘close’, but no methods.

Is the method on MainScreen marked as Public?

Just FYI - autocomplete is wrong sometimes. If you’re sure the thing is right, type it and Run. The compiler will tell you what’s actually going on.

1 Like

How is Parent defined? If it is simply Window, then you need to cast it to the proper window type in order to call the method.