I'm new to Xamarin iOS developement and am having an issue that I can't figure out. When the following line of code runs, it throws a null reference exception and I can't figure out why.
this.NavigationController.PushViewController(location, true);
Here is my whole block of code.
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.");
}
}
Because your login viewController is not in the NavigationController, So When you use this.NavigationController , it will throw null reference exception.
To solve this problem, two ways here:
Put login ViewVontroller into NavigationController, make NavigationController(with the rootviewController of login page) become the initial Controller of storyboard.
change this code this.NavigationController.PushViewController(location, true);
To this.PresentViewController(location, true, null);