我向故事板添加了一個UIViewController,並試圖訪問它,並按照下面的代碼將它展示出來。但由於某些原因,它被讀爲NULL。 雖然此代碼工作正常。我調試了他們兩個,我得到空爲wcv
並得到acv
的值,是的。如果我檢查故事板文件的XML代碼,我可以找到視圖控制器有兩個是IDS StoyboardId和RestorationId添加了一個UIViewController到故事板,但無法在xamarin中以編程方式讀取它ios
注:相同的導航代碼工作其他地方。我使用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);
}
}
StoryBoardCode SS
故事板代碼爲WelcomeScreenController2
<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";
}
}
請問您可以發佈WelcomeScreenController2和ActivityViewController2類的聲明以及它們的構造函數嗎? – 2014-11-21 10:51:48
是的。當然。我現在會這樣做。 – 2014-11-21 10:53:39
@DimitrisTavlikos添加了聲明。 – 2014-11-21 11:00:38