ios - 向 Storyboard添加了 UIViewController,但无法在 xamarin ios 中以编程方式读取它

标签 ios iphone uiviewcontroller xamarin.ios xamarin

我在 Storyboard中添加了一个 UIViewController 并尝试访问它并将其显示出来,按照下面编写的代码。但由于某些原因,它被读取为 NULL。 虽然这段代码运行良好。我调试了它们,我得到了 wcv 的 null 值,并得到了 acv 的值,是的。如果我检查 Storyboard 文件的 xml 代码,我可以找到 View Controller ,其 ID 为 StoyboardIdRestorationId

注意: 相同的导航代码适用于其他地方。我正在使用 Xamarin-iOS(独立许可证)

WelcomeScreenController2 wcv =(UIApplication.SharedApplication.Delegate as AppDelegate).StoryBoard.InstantiateViewController ("WelcomeScreenController2") as WelcomeScreenController2;
                                        this.NavigationController.PushViewController(wcv,true);




ActivityViewController2 acv = (UIApplication.SharedApplication.Delegate as AppDelegate).StoryBoard.InstantiateViewController ("ActivityViewController2") as ActivityViewController2;
                                        this.NavigationController.PushViewController (acv, true);

两者的区别在于,ActivityViewController2继承了BaseController,而WelcomeScreenController2继承了UIViewController

BaseController代码

public partial class BaseController : UIViewController
    {
        // provide access to the sidebar controller to all inheriting controllers
        protected SidebarController SidebarController { 
            get {
                return (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.SidebarController;
            } 
        }

        // provide access to the sidebar controller to all inheriting controllers
        protected NavController  NavController { 
            get {
                return (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.NavController;
            } 
        }

        public BaseController (IntPtr handle) : base (handle)
        {
        }
        public BaseController(string nibName, NSBundle bundle) : base(nibName, bundle)
        {
        }



        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            NavigationItem.SetLeftBarButtonItem(
                new UIBarButtonItem(UIImage.FromBundle("menu-icon")
                    , UIBarButtonItemStyle.Plain
                    , (sender,args) => {
                        SidebarController.ToggleMenu();
                    }), true);
        }

        public override void ViewWillAppear (bool animated)
        {   
            base.ViewWillAppear (animated);

        }

        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);
        }

        public override void ViewWillDisappear (bool animated)
        {
            base.ViewWillDisappear (animated);
        }

        public override void ViewDidDisappear (bool animated)
        {
            base.ViewDidDisappear (animated);
        }

    }

Storyboard代码 SS

Code from the Storyboard

WelcomeScreenController2 的 StoryBoard 代码

<scene sceneID="684">
            <objects>
                <viewController id="685" sceneMemberID="viewController" customClass="WelcomeScreenController2" storyboardIdentifier="WelcomeScreenController2" restorationIdentifier="WelcomeScreenController2">
                    <view key="view" contentMode="scaleToFill" id="686">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="687" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="4000" y="135"/>
        </scene>

WelcomeScreen2 类

public partial class WelcomeScreenController2 : UIViewController
    {
        public WelcomeScreenController2 (IntPtr handle) : base (handle)
        {
        }
}

ActivityViewController2 类

public partial class ActivityViewController2 : BaseController
    {
public ActivityViewController2 (IntPtr handle) : base (handle)
        {
            Title = "ACTIVITY";
        }

        public ActivityViewController2 () : base ((string)null, null)
        {
            Title = "ACTIVITY";
        }
}

最佳答案

它不需要我改变任何东西。只需清洁解决方案并再次调试/运行,这对我有用。

但正如@Dimitris 在评论中所写,这可能是选角问题。那为什么我确切地说它是新的 UIViewController 而不是旧的,清理与它有什么关系,直到清理后它运行良好?

关于ios - 向 Storyboard添加了 UIViewController,但无法在 xamarin ios 中以编程方式读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27059131/

相关文章:

ios - (Swift) ViewController 的 UI 元素无法通过实现的委托(delegate)方法进行编辑

ios - 如何在 Swift 中 popToViewController 未初始化?

iphone - 从我的 iPhone 应用程序中使用的 iTunes 库中删除/隐藏文件

iphone - 从坞站连接器捕获音频

ios - 从 NSManaged 对象中过滤掉整数

iphone - iOS:CGRectMake 不会改变 UILabel 的位置

php - 用htaccess密码保护识别 "apple-touch-icon"

iphone - 如何在复杂的导航堆栈之间传递变量

ios - 向 nsdictionary 添加各种对象和键

ios - NSNotificationCenter 无法将事件指示器设置为隐藏