ios - Xamarin iOS PushViewController 空引用异常

标签 ios xamarin xamarin.ios uinavigationcontroller nullreferenceexception

我是 Xamarin iOS 开发的新手,遇到了一个我无法解决的问题。当下面这行代码运行时,它会抛出一个空引用异常,我不明白为什么。

this.NavigationController.PushViewController(location, true);

这是我的整个代码块。

partial void BtnLogin_TouchUpInside(UIButton sender)
    {
        if (txtPassword.Text != "" && txtUsername.Text != "")
        {
            //send UN & PD to webservice for validation.
            xxx.xxx.xxx.Services s = new xxx.xxx.xxx.Services();
            bool result = s.validateLogin(txtUsername.Text, txtPassword.Text);
            if (result)
            {
                try
                {
                    LocationViewController location = this.Storyboard.InstantiateViewController("LocationViewController") as LocationViewController;

                    if (location != null)
                        this.NavigationController.PushViewController(location, true);
                }
                catch (Exception ex)
                {
                    LoginFail("Error", ex.Message);
                }
            }
            else
            {
                LoginFail("Login", "Invalid username or password.");
            }
        }
        else
        {
            LoginFail("Login", "You must enter both your username and password.");
        }
    }

And my Storyboard

最佳答案

因为你登录的viewController不在NavigationController中,所以当你使用this.NavigationController时,会抛出空引用异常。

解决这个问题,这里有两种方法:

  • 将登录ViewVontroller放到NavigationController中,使NavigationController(和登录页面的rootviewController)成为storyboard的初始Controller。

  • 更改此代码 this.NavigationController.PushViewController(location, true);

    this.PresentViewController(location, true, null);

关于ios - Xamarin iOS PushViewController 空引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45201728/

相关文章:

ios - 找出已安装的 iPhone SDK 版本的最佳方法是什么?

c# - WebSockets-尖锐异常: WebSocketException

c# - MonoTouch 中的图片库滑动 UI

ios - 设置 UICollectionViewDelegateFlowLayout Xamarin iOS F#

iphone - 如何删除 UITabBarItem 下方的空间?

ios - 台风初始化程序注入(inject)和调用 super.init()

c# - 无法从 xamarin 中的 TableView RowSelected 加载 ViewController

c# - 是否建议为 Xamarin 项目始终选择目标 AnyCPU?

xamarin.ios - 在 MonoTouch 中更改每个配置的显示名称

ios - 应用程序不显示警报 View Controller ?