2013-12-17 38 views
0

我正在使用組件顯示彈出窗口並使用事件偵聽器來獲取彈出窗口屬性並刪除父窗口中的彈出窗口。然而,在監聽器popup var中,poup var是nul,所以會引發錯誤。無法在FlashBuilder中使用事件從子彈出窗口中檢索屬性

任何建議將不勝感激。

約翰


這裏是我的EditStudentLogInForm.mxml組件..

<?xml version="1.0"?> 
<!-- containers\layouts\myComponents\MyLoginForm.mxml --> 
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" 
      creationComplete="handleCreationComplete();"> 
    <mx:Script> 
     <![CDATA[ 
      import mx.managers.PopUpManager; 
      [Bindable] public var studentLoginEmail:String; 
     ]]> 
    </mx:Script> 

    <mx:Form width="333"> 
     <mx:FormItem label="Email"> 
      <mx:TextInput id="username" width="207"/> 
     </mx:FormItem> 
     <mx:FormItem label="Password"> 
      <mx:TextInput id="password" 
         width="205"/> 
     </mx:FormItem> 
    </mx:Form> 
    <mx:HBox> 
     <mx:Button id="okButton" label="OK"/> 
     <mx:Button id="cancelButton" label="Cancel" /> 
    </mx:HBox> 
</mx:TitleWindow> 

這裏是父...

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
      xmlns:s="library://ns.adobe.com/flex/spark">  
    <mx:Script> 
     <![CDATA[ 
      import flash.events.Event; 
      import mx.managers.PopUpManager; 
      import mx.core.IFlexDisplayObject; 
      import EditStudentLogInForm; 
      import mx.containers.TitleWindow; 

      public var helpWindow:EditStudentLogInForm; 

      public function showLogin():void { 
        // Create the TitleWindow container. 
       var helpWindow:EditStudentLogInForm = EditStudentLogInForm(
        PopUpManager.createPopUp(this, EditStudentLogInForm, true)); 

       helpWindow.username.text = "[email protected]"; 
       helpWindow["cancelButton"].addEventListener("click", removeMe); 
       helpWindow["okButton"].addEventListener("click", submitData); 
      } 

      // OK button click event listener. 
      private function submitData(event:Event):void { 
       testText.text = helpWindow.username.text; 
           //*********helpWindow is nul******* 
       removeMe(event); 
      } 

      // Cancel button click event listener. 
      private function removeMe(event:Event):void { 
       PopUpManager.removePopUp(helpWindow); 
      }       
     ]]> 
    </mx:Script>  
</mx:Application> 

回答

1

當你

public function showLogin():void { 
    var helpWindow:EditStudentLogInForm = ... 
} 

您正在聲明並實例化showLogin方法範圍內的新變量helpWindow。這意味着您在showLogin方法之外無法訪問您分配給本地作用域變量的實例。

你做聲明在類範圍的另一個變量helpWindow(類是在這種情況下,主應用程序),你永遠給予任何實例,它(因爲你賦予這個彈出實例的helpWindow 。變量,只生活在showLogin

因此,當您嘗試另一種方法來訪問這個變量,它的值是null
解決辦法很簡單:只是分配彈出實例類範圍的變量:

public function showLogin():void { 
    helpWindow = EditStudentLogInForm(
     PopUpManager.createPopUp(this, EditStudentLogInForm, true) 
    ); 
    ... 
} 

在一個側面說明:如果你有相同名稱的變量的類和方法中,最局部範圍的一個總是優先:

public var s:String = 'class'; 

public function myMethod():void { 
    var s:String = 'method'; 
    trace(s);  // prints method 
    trace(this.s); // prints class 
} 

public function myOtherMethod():void { 
    trace(s);  // prints class 
    trace(this.s); // prints class 
} 
+0

當然!凌晨2點。我醒了,但我想我的大腦不是。謝謝。 – user278859

相關問題