【问题标题】:Can't change viewController with button in Xamarin无法在 Xamarin 中使用按钮更改 viewController
【发布时间】:2017-06-10 22:49:00
【问题描述】:

我正在尝试通过单击按钮打开一个新的 ViewController。我创建了一个新的 secondViewController 类并将名称设置为 SecondViewController。我还添加了一个导航控制器并将其与 RootView 控制器连接。但是,如果我尝试运行它,我会收到以下错误:

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

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

我是否遗漏了什么,名称是否与 Storyboard Id 相同?

【问题讨论】:

    标签: c# xamarin xamarin.ios storyboard


    【解决方案1】:

    看起来您从未为您的secondViewController 创建单独的类。 确保你明确地创建了一个新的UIViewController 类:

    public partial class SecondViewController : UIViewController
    {
    }
    

    然后在您的故事板中,将您的 secondViewController 的“类”设置为此类。

    这将让您将InstantiateViewController 转换为您的SecondViewController

    或者,您可以将代码更改为:

            btn_Next.TouchUpInside += (object sender, EventArgs e) =>{
    
                var controller = this.Storyboard.InstantiateViewController("SecondViewController");
                this.NavigationController.PushViewController(controller, true);
            };
    

    如果您不希望为新控制器设置一个单独的类。

    【讨论】:

    • 所以我创建了新的 UIViewController 类,将该类添加到 secondViewController 并将该类实例化为 SecondViewController。但是我仍然收到无法找到 SecondViewController 的错误。我指定了名称,但找不到设置情节提要 ID 的方法。
    • 当您在故事板中选择 UIVeiwController 时(通过单击底部边距),打开属性选项卡,并且有一个“故事板 ID”字段。就在“类”和“模块”下
    • 哦,谢谢找到了。我总是创建一个 UIView 类来指定视图,所以我看不到“Storyboard id”属性。
    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2021-02-10
    • 1970-01-01
    • 2020-10-24
    相关资源
    最近更新 更多