2013-11-09 52 views
3

我想這樣做,當我按下我的UITableView行時,它會發送一些數據到另一個UIViewController並使一個seque。RowSelected - NullReferenceException

然而測試出來的時候,它給了我:

System.NullReferenceException已經拋出的對象引用未設置 爲對象

的錯誤似乎在這裏展示的實例:

viewController.NavigationController.PushViewController (detail, true); 

這裏是我的 「RowSelected」

public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
{ 
    Console.WriteLine ("Row: " + tableItems [indexPath.Row].Heading); 

    //new UIAlertView("Row Selected", tableItems[indexPath.Row].Heading, null, "OK", null).Show(); 
    tableView.DeselectRow (indexPath, true); 

    // Specially for Storyboard !! 
    var detail = viewController.Storyboard.InstantiateViewController("detail") as HolsteinItemViewController; 
    detail.Title = tableItems[indexPath.Row].Heading; 
    detail.LoadUrl (tableItems[indexPath.Row].SubHeading); 
    viewController.NavigationController.PushViewController (detail, true); 
} 

捕獲到異常:

System.NullReferenceException: Object reference not set to an instance of an object 
    at Holstein.TableSource.RowSelected (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x0008e] in /Users/Emil/Projects/Holstein/Holstein/Code/TableSource.cs:41 
    at 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 Holstein.Application.Main (System.String[] args) [0x00008] in /Users/Emil/Projects/Holstein/Holstein/Main.cs:16 

TableSource.cs線46

viewController.NavigationController.PushViewController (detail, true); 

Main.cs第16行

UIApplication.Main (args, null, "AppDelegate"); 
+0

是否tableItems [indexPath.Row] .Heading和Subheading不爲null? – user1646737

+0

兩者都應該設置,因爲它們都顯示在單元格 –

+0

是否有一些其他屬性的細節是預期的,但沒有定義? – user1646737

回答

4

viewController.NavigationController PROPERT y爲null。據到iOS UIViewController參考here

如果接收器或其祖先之一是導航 控制器的孩子,這個屬性包含擁有導航控制器。 如果視圖控制器未嵌入 導航控制器內,則此屬性爲零。

顯然,該視圖控制器不是UINavigationController堆棧的一部分。

+0

如何將它添加到堆棧? – testing

+1

通過調用UINavigationController的PushViewController方法。 –

+0

http://stackoverflow.com/questions/36324571/object-reference-not-set-to-an-instance-of-an-object-in-monotouch請回答這個問題 –