我在 Storyboard中添加了一个 UIViewController 并尝试访问它并将其显示出来,按照下面编写的代码。但由于某些原因,它被读取为 NULL。
虽然这段代码运行良好。我调试了它们,我得到了 wcv
的 null 值,并得到了 acv
的值,是的。如果我检查 Storyboard 文件的 xml 代码,我可以找到 View Controller ,其 ID 为 StoyboardId 和 RestorationId
注意: 相同的导航代码适用于其他地方。我正在使用 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
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/