Reputation: 33
I am sure this is probably a very simple solution, but I am quite new to xamarin and c# in general. I have created a tableview, and when a cell in that view is pressed I want it to navigate to a new view, named SecondViewController(). I have the following class in my ViewController.cs
public void changeview()
{
SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;
this.NavigationController.PushViewController(controller, true);
}
Then in my TableSource.cs class, I have the following code to call changeview()
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
ViewController view = new ViewController();
view.changeview();
tableView.DeselectRow(indexPath, true);
}
I get no errors when it compiles, but when it runs I get an error on the following code
SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;
which reads
System.NullReferenceException: Object reference not set to an instance of an object
why does this not work? if I use this same code within a button class it works great, to test to make sure I was calling the class correctly I changed the code within changeview() to a UIAlertView and when a cell was pressed the alertview worked. I am not sure where to go from here, possibly there is a better method for changing views for this purpose? any help is greatly appreciated!
Upvotes: 1
Views: 944
Reputation: 2402
SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;
Do you used Storyboard Id for "SecondViewController". If not goto stroyboard add storyboard Id to your SecondViewController
Upvotes: 1
Reputation: 156
You should keep a reference of your ViewController in your TableSource.cs class as
public ViewController ParentController{ get; set;}
When initializing the data source,
tableClassObject.ParentController = this;
In Row Selected, it should be
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow(indexPath, true);
ParentController.changeview();
}
This is simplest solution.
Upvotes: 0
Reputation: 293
You will need to have a global reference or access to the current active navigation controller of your application in order to make a push.
What you can do is to create an instance of an AppDelegate
and set the Navigation Controller to a variable.
In your AppDelegate.cs
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
//application layer declarations
public UINavigationController navCtrl { get; set; }
/*
.
. the rest of the codes
.
.*/
}
In your ViewController.cs (First view) or the root view controller where you instantiated the navigation controller:
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
.
*/
}
In your TableSource.cs Class.
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);
}
This should be able to do what you want.
Upvotes: 0