在tableviewsource上选择的Monodevelop行会导致空引用异常

时间:2013-02-14 19:18:02

标签: ios uitableview drop-down-menu xamarin.ios uipopovercontroller

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是我可以点击实际按钮的类。

我有例外
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);

1 个答案:

答案 0 :(得分:1)

您正在RowSelected方法中创建DetailViewController的新实例。您需要的是对已存在的DetailViewController的引用。

创建TableViewController时,需要传入对“parent”DetailViewController的引用。在您的RowSelected方法中,您可以在现有的DVC上调用方法。