如何从对话框中正确创建子对话框?

问题描述 投票:0回答:1

UI启动器的两种方式有什么区别?

我想创建子对话框。通过使用下面的第一个代码,我可以正确地实现我的要求。仅当我单击按钮时才会出现子对话框。

但是如果我使用第二个代码(使用构造函数),则当我运行脚本时会出现子对话框。

这两种方法有什么区别?

另外:运行第三个示例给我一个错误。为什么?

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")
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")

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")
dialog dm-script
1个回答
1
投票

我不太明白你的问题。在你的第二个代码中,你在构造函数中call

self.Display
,所以对话框自然会立即显示?

也许您需要更好地理解

constructor
方法的概念? 在内存中创建对象时,即会调用此方法(即运行代码),即当您调用
Alloc(mySub)
时,您会在内存中创建“mySub”类的对象,该对象会调用其构造函数。


我添加了一个您可能想做的示例: 当主脚本仍在范围内时分配子对话框,但不显示它。相反,将其作为主对话框的成员变量,并在按下按钮时显示(在“OnButtonPressed”操作方法中)。

有不同的方法可以完成:

  1. 在主线程上分配并将对象作为参数传递给主对话框(在主脚本上),如下所示:
// On the main script:
object sub = Alloc(sub)
object main = Alloc(main)
main.SetSubDlg(sub)   // SetSubDlg is a custom method that stores into a member variable
main.LaunchDisplay()  // LaunchDisplay is a custom method that shows the main dialog.
  1. 在主对话框对象的构造函数中分配子对话框。由于主对话框是通过对主脚本的调用来分配的,因此当脚本代码仍在范围内时,会立即调用其构造函数。 我更喜欢这种方式,因为它更干净地封装所有内容。
class CSubDialog : UIframe
{
    number CreateAndPose(object self)
    {
        TagGroup dlg,dlgitems
        dlg = DLGCreateDialog("SubDialog",dlgitems)
        dlgItems.DLGAddElement(DLGCreateLabel("I'm the sub dialog."))
        return self.init(dlg).pose()
    }
}

class CMainDialog : UIframe
{
    object subDlg
    CMainDialog(object self)
    {
        // The constructor of CMainDialg is called as soon as the 
        // Alloc(CMainDialog) in the main program is called.
        // At this time, the whole script code is still in scope
        // so the subDLG can be allocated. Just hold it as member
        // of this dialog.
        subDlg = Alloc(CSubDialog)
    }
    
    void OnLaunchSub(object self)
    {
        // Safer coding: Check that the member variable is 
        // holding a valid object. 
        if ( !subDlg.ScriptObjectIsValid() ) throw( "No Sub dialog found." )
        // Further safety, check if is of the correct class (All lower case!)
        if ("csubdialog" != subDLG.ScriptObjectGetClassName()) throw( "Wrong object." )
        subDLG.CreateAndPose()      
    }
    
    object CreateAndDisplay(object self)
    {
        TagGroup dlg,dlgitems
        dlg = DLGCreateDialog("Main Dialog",dlgitems)
        dlgItems.DLGAddElement(DLGCreateLabel("I'm the main dialog."))
        dlgItems.DLGAddElement(DLGCreatePushButton("Launch","OnLaunchSub"))
        self.init(dlg).display("Main Dialog")
        return self
    }
    
}

Alloc(CMainDialog).CreateAndDisplay()
© www.soinside.com 2019 - 2024. All rights reserved.