我是 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.");
}
}
最佳答案
因为你登录的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/