【问题标题】:Xamarin IOS Navigation with TableViewXamarin IOS 导航与 TableView
【发布时间】:2015-12-20 16:49:30
【问题描述】:

我确信这可能是一个非常简单的解决方案,但总的来说,我对 xamarin 和 c# 还是很陌生。我创建了一个表格视图,当按下该视图中的一个单元格时,我希望它导航到一个名为 SecondViewController() 的新视图。我的 ViewController.cs 中有以下类

     public void changeview()
    {

           SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;
        this.NavigationController.PushViewController(controller, true);
    }

然后在我的 TableSource.cs 类中,我有以下代码来调用 changeview()

   public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {

        ViewController view = new ViewController();
        view.changeview();
        tableView.DeselectRow(indexPath, true);
    }

编译时没有错误,但运行时出现以下代码错误

   SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;

读取

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

为什么这不起作用?如果我在按钮类中使用相同的代码,它效果很好,为了测试以确保我正确调用了类,我将 changeview() 中的代码更改为 UIAlertView,当按下单元格时,alertview 工作。我不知道从这里去哪里,可能有更好的方法来为此目的改变视图?非常感谢任何帮助!

【问题讨论】:

    标签: c# uitableview xamarin uinavigationcontroller


    【解决方案1】:
           SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;
    

    您是否将 Storyboard Id 用于“SecondViewController”。如果没有转到 stroyboard,请将情节提要 ID 添加到您的 SecondViewController

    【讨论】:

      【解决方案2】:

      您需要拥有全局引用或访问应用程序当前活动的导航控制器才能进行推送。

      您可以做的是创建一个AppDelegate 的实例并将导航控制器设置为一个变量。

      在您的 AppDelegate.cs 中

      [Register ("AppDelegate")]
      public partial class AppDelegate : UIApplicationDelegate
      {
          //application layer declarations
          public UINavigationController navCtrl { get; set; }
          /*
          .
          .  the rest of the codes
          .
          .*/
      }
      

      在您的 ViewController.cs(第一个视图)或您实例化导航控制器的根视图控制器中:

      public override void ViewDidLoad() {
          AppDelegate appD = UIApplication.SharedApplication.Delegate as AppDelegate;
      
          // assign the UINavigationController to the variable being used to push
          // assuming that you have already initialised or instantiated a UINavigationController
          appD.navCtrl = this.NavigationController; // Your UINavigation controller should be here
          /*
          .
          . the rest of the codes
          .
          */
      }
      

      在您的 TableSource.cs 类中。

      private AppDelegate appD = UIApplication.SharedApplication.Delegate as AppDelegate;
      
      public override void RowSelected (UITableView tableView, NSIndexPath indexPath){
          SecondViewController secondView = UIStoryboard.FromName ("Main", null).InstantiateViewController ("SecondViewController") as SecondViewController;
          appD.navCtrl.PushViewController (secondView, true);
      }
      

      这应该可以做你想做的事。

      【讨论】:

      • 我刚刚尝试了这段代码,它现在给我“发生了未处理的异常”。我不知道为什么。在你的 someFunction() 中这两行之后是否应该有更多的代码,或者这一切都应该在函数中?有什么东西可以调用这个函数吗?非常感谢您的回复!
      • 您能否更新问题中的代码示例并将您的第一个视图控制器包含在您使用导航控制器的位置?实际上,您是否在某处初始化了 UINavigationController 对象?在我的示例中, someFunction() 应该是您自己的函数,例如 viewDidLoad() 。我更新了我的示例以消除混淆。
      【解决方案3】:

      您应该在 TableSource.cs 类中保留对 ViewController 的引用

      public ViewController ParentController{ get; set;}
      

      初始化数据源时,

      tableClassObject.ParentController = this;
      

      在Row Selected中,应该是

      public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
          {
              tableView.DeselectRow(indexPath, true);
              ParentController.changeview();
          }
      

      这是最简单的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-14
        • 2019-07-24
        • 1970-01-01
        • 1970-01-01
        • 2016-06-06
        • 1970-01-01
        • 2021-11-19
        • 2017-01-21
        相关资源
        最近更新 更多