What's the difference for the two methods of UI initiator?
I’m building a UI with a sub-dialog and need it to appear only when a user clicks a button. However, I’m encountering different behaviors with two different approaches.
Method 1: Delayed Creation (Works as Expected)
In this method, I create and show the sub-dialog only when the button is clicked:
Class MySub:UIFrame
{
Object Init(Object self)
{
TagGroup DLGitems = DLGCreateDialog("Sub")
TagGroup RadioList=DLGCreateRadioList()
RadioList.DLGAddRadioItem("option 1",1)
RadioList.DLGAddRadioItem("option 2",2)
RadioList.DLGAddRadioItem("option 3",3)
DLGitems.DLGAddElement(RadioList)
Return self.super.Init(DLGitems)
}
}
Class MyMain:UIFrame
{
Object SubDialog
Void CallSubDialog(Object self)
{
Result("\nOpen sub-dialog.")
SubDialog.Pose() // call the local object
self.LookUpElement("Label").DLGTitle("option chosen")
}
Object Init(Object self, Object SDpass)
{
SubDialog = SDpass // Keep the object in a local variabl
TagGroup DLGitems = DLGCreateDialog("Main")
DLGitems.DLGAddElement(DLGCreateLabel("choose option").DLGIdentifier("Label"))
DLGitems.DLGAddElement(DLGCreatePushButton("Options","CallSubDialog"))
Return self.super.Init(DLGitems)
}
}
Object SubDialogOBJ = Alloc(MySub).Init() // Initialize the sub-dialog object in the main script
Object DialogOBJ = Alloc(MyMain).Init(SubDialogOBJ) // Pass on the object into the other
DialogOBJ.Display("Dialog")
Method 2: Immediate Creation in the Constructor (Unexpected Behavior)
Here, I initialize and show the sub-dialog in the constructor, which causes the sub-dialog to display as soon as the script runs:
Class MySub:UIFrame
{
TagGroup gen_MySubUI_dlg(Object self)
{
TagGroup DLGitems = DLGCreateDialog("Sub")
TagGroup RadioList=DLGCreateRadioList()
RadioList.DLGAddRadioItem("option 1",1)
RadioList.DLGAddRadioItem("option 2",2)
RadioList.DLGAddRadioItem("option 3",3)
DLGitems.DLGAddElement(RadioList)
Return DLGitems
}
MySub(Object self)
{
self.init(self.gen_MySubUI_dlg())
self.Display("MySubUI")
}
~MySub(Object self)
{
Result("Quit" + "\n")
}
}
Class MyMain:UIFrame
{
Object SubDialog
Void CallSubDialog(Object self)
{
Result("\nOpen sub-dialog.")
SubDialog.Pose() // call the local object
self.LookUpElement("Label").DLGTitle("option chosen")
}
Object Init(Object self, Object SDpass)
{
SubDialog = SDpass // Keep the object in a local variabl
TagGroup DLGitems = DLGCreateDialog("Main")
DLGitems.DLGAddElement(DLGCreateLabel("choose option").DLGIdentifier("Label"))
DLGitems.DLGAddElement(DLGCreatePushButton("Options","CallSubDialog"))
Return self.super.Init(DLGitems)
}
}
Object SubDialogOBJ = Alloc(MySub) // Initialize the sub-dialog object in the main script
Object DialogOBJ = Alloc(MyMain).Init(SubDialogOBJ) // Pass on the object into the other
DialogOBJ.Display("Dialog")
Questions:
Primary Question:
- Why do these two methods behave differently? Specifically, why does the sub-dialog in Method 2 appear immediately upon running the script instead of waiting for the button click?
Additional Question:
- When running a third example (not included here), I receive the following error message:
Class MySub:UIFrame
{
TagGroup gen_MySubUI_dlg(Object self)
{
TagGroup DLGitems = DLGCreateDialog("Sub")
TagGroup RadioList=DLGCreateRadioList()
RadioList.DLGAddRadioItem("option 1",1)
RadioList.DLGAddRadioItem("option 2",2)
RadioList.DLGAddRadioItem("option 3",3)
DLGitems.DLGAddElement(RadioList)
Return DLGitems
}
MySub(Object self)
{
self.init(self.gen_MySubUI_dlg())
self.Display("MySubUI")
}
~MySub(Object self)
{
Result("Quit" + "\n")
}
}
Class MyMain:UIFrame
{
Object SubDialog
Void CallSubDialog(Object self)
{
Result("\nOpen sub-dialog.")
Alloc(MySub) // call the local object
}
Object Init(Object self)
{ // Keep the object in a local variabl
TagGroup DLGitems = DLGCreateDialog("Main")
DLGitems.DLGAddElement(DLGCreateLabel("choose option").DLGIdentifier("Label"))
DLGitems.DLGAddElement(DLGCreatePushButton("Options","CallSubDialog"))
Return self.super.Init(DLGitems)
}
}
Object DialogOBJ = Alloc(MyMain).Init() // Pass on the object into the other
DialogOBJ.Display("Dialog")
What I’ve Tried:
- Verified that the button click event is correctly connected in Method 1, which behaves as expected.
- Reviewed the sub-dialog’s constructor and display logic without finding any obvious issues.
Any insights into the differences in these initialization methods and help identifying the error in the third example would be greatly appreciated.