简体   繁体   中英

Can't change viewController with button in Xamarin

I am trying to open a new ViewController with a button click. I created a new secondViewController class and set the name to SecondViewController. I also added an Navigation Controller and connected it with the RootView Controller. However if I try to run it i get the following errors: 在此处输入图片说明

   btn_Next.TouchUpInside += (object sender, EventArgs e) =>{

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

Did I miss something and is the Name the same thing like the Storyboard Id?

Looks like you never created a separate class for your secondViewController . Make sure you explicitly create a new UIViewControlle r class:

public partial class SecondViewController : UIViewController
{
}

Then in your storyboard, set the "Class" of your secondViewController to this class.

This will let you cast InstantiateViewController to your SecondViewController .

Alternatively, you can just change your code to this:

        btn_Next.TouchUpInside += (object sender, EventArgs e) =>{

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

If you don't care for having a separate class for your new controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM