0
public delegate bool SelectedValueForDropDown (string name); 
public partial class BasicTableViewSource : UITableViewSource 
{ 
public override void RowSelected(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) 
    { 
     SelectedValueForDropDown selValue; 
     // Do something for the selected row 
     string itemSel = this._tableItems[indexPath.Section]; 
     Console.WriteLine("90 the item selected is :{0}",itemSel); 

     DetailViewController dvc = new DetailViewController(); 
     selValue = dvc.SelectedValueForPosition; 
     //bool val = dvc.SelectedValueForPosition(itemSel); 
     ValueSelectedDD valDD = new ValueSelectedDD(); 

     bool success = valDD.HandleValueSelected(selValue); 
     Console.WriteLine("100 the value is : {0}",success); 
    } 
..... 
} //class closed 

public class ValueSelectedDD 
{ 
    public bool HandleValueSelected (SelectedValueForDropDown selValue) 
    { 
     bool success =false; 
     success = selValue("CEO"); 

     return success; 
    } 
} 
//This method is in DetailViewController.designer.cs class. 
public bool SelectedValueForPosition (string name) 
    { 
     Console.WriteLine("the value selected is :{0}",name); 
     btnPosition.SetTitle(name,UIControlState.Normal); //Exception occurs here 
     return true; 
    } 

我正在處理簡單的MonoDevelop項目。所以我有一個tableview作爲我的按鈕彈出。我點擊它,我有3個選項。我選擇一個,按鈕應該用選擇的選項替換它的標籤。所選的行在tableviewsource類中。 DetailViewController是我有實際的按鈕被點擊的類。我收到一個例外在tableviewsource上選擇的Monodevelop行導致null引用異常

System.NullReferenceException: Object reference not set to an instance of an object 

at EmployeeWithDropDown.DetailViewController.SelectedValueForPosition (System.String name) [0x0000b] in /Users/.../EmployeeWithDropDown/EmployeeWithDropDown/DetailViewController.designer.cs:106 

at EmployeeWithDropDown.ValueSelectedDD.HandleValueSelected (EmployeeWithDropDown.SelectedValueForDropDown selValue) [0x00002] in /Users/.../EmployeeWithDropDown/EmployeeWithDropDown/BasicTableViewSource.cs:109 
    at EmployeeWithDropDown.BasicTableViewSource.RowSelected (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00036] in /Users/.../EmployeeWithDropDown/EmployeeWithDropDown/BasicTableViewSource.cs:99 

at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 
    at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
    at EmployeeWithDropDown.Application.Main (System.String[] args) [0x00000] in /Users/.../EmployeeWithDropDown/EmployeeWithDropDown/Main.cs:17 

System.NullReferenceException has been thrown. 
Object Reference not set to an instance of an object. 

如果您需要更多信息,請詢問。謝謝。

編輯:

public partial class PopoverContentViewcontroller : UIViewController 
{ 
    public override void ViewDidLoad() 
    { 

     base.ViewDidLoad(); 
     table = new UITableView(View.Bounds); 
     table.AutoresizingMask = UIViewAutoresizing.All; 
     string[] tableItems = new string[]{"CEO","Developer","IT"}; 
     //List<String> wordList = Arrays.asList(words); 
     List<string> lst = new List<string>(tableItems); 

     table.Source = new BasicTableViewSource(lst); 
     Add (table); 
     } 
..... 
} 

這是填充表下拉popovercontroller。即價值在這裏得到填充在下拉..

EDIT2:

    DetailViewController dvc; 
        ..... 
      table.Source = new BasicTableViewSource(lst,dvc); 
     Add (table); 
+0

這很混亂。 DetailViewController是父視圖還是tableview的子視圖(誰調用誰?)。在RowSelected中,您()創建一個DVC的新實例,但不顯示它。如果btnPosition是DVC上的UI元素,如果DVC尚未渲染,則它可能不存在(即,它爲NULL)。 – Jason 2013-02-15 00:48:58

+0

@Jason。我添加了一些代碼。所以我在DetailViewController上有了btnPosition。點擊它從Popoverviewcontentcontroller填充一個uitableviewcontroller。然後選擇一行。在basictableviewsource中選擇的行被調用。例如,我拿到了「首席執行官」的價值。然後,我的意圖是從Rowselected啓動一個委託給DetailViewController類,然後將所選值作爲按鈕上的標籤。那有意義嗎? – RookieAppler 2013-02-15 01:31:47

回答

1

你在你的RowSelected方法創建DetailViewController的新實例。你需要的是對已經存在的DetailViewController的引用。

當你創建你的TableViewController時,你需要傳遞一個對「父」DetailViewController的引用。在你的RowSelected方法中,你可以在已經存在的DVC上調用一個方法。

+0

我在代碼中通過名稱EDIT2做了一個新的更改。我將DetailViewController的引用傳遞給了我的BasicTableViewSource,但仍然顯示異常。我在RowSelected中使用了這個引用,我得到了這個異常:NSInvalidArgumentException原因: - [DetailViewController btnPositionClick:]:無法識別的選擇器發送到實例。你能解釋一下嗎?或者指出我錯在哪裏。 – RookieAppler 2013-02-15 17:40:16

+0

我不認爲我可以在沒有看到更多代碼的情況下回答這個問題。我建議把它移到Xamarin論壇上,這更適合擴展討論。 – Jason 2013-02-15 20:10:15

+0

我昨天自己做到了。但也沒有答覆。 – RookieAppler 2013-02-15 20:21:42

相關問題