2017-10-24 5 views
1

私はこれを数時間よく熟考しましたが、無駄です。 120秒間何も操作がない場合はいつでも、起動画面に戻す方法をiPadアプリに追加しようとしています。クラスを継承するUIApplication内からのナビゲーション

私はXamarinとIOSプログラミングの両方に非常に新しいので、私が間違った角度からこれに近づいている場合は謝ります。 私はこのタイマーが正常に動作します。この

[Register("ElectronicReceptionMain")] 
public class ElectronicReceptionMain : UIApplication 
{  
    public override void SendEvent(UIEvent uievent) 
    { 
     Debug.WriteLine("SendEvent");    
     var allTouches = uievent.AllTouches; 
     if(allTouches.Count > 0) 
     { 
      ResetIdleTimer(); 
     }    
     base.SendEvent(uievent); 
    } 

    NSTimer idleTimer; 
    void ResetIdleTimer() 
    {    
     idleTimer = NSTimer.CreateScheduledTimer(TimeSpan.FromSeconds(120), RefreshScreen); 
    } 

    void RefreshScreen(NSTimer obj) 
    { 
     Debug.WriteLine("Elapsed");      
     UIStoryboard StoryBoard = UIStoryboard.FromName("Main", null); 
     ViewController uvc = StoryBoard.InstantiateViewController("StartScreenController") as ViewController; 
     SharedApplication.KeyWindow.RootViewController.NavigationController.PushViewController(uvc, true); 

    } 
} 

のように見えるクラスを作成している、(デバッグメッセージは必ずプリントアウト)。しかし、NavigationControllerをnull以外のものにすることはできません。明らかにnull参照例外が発生します。

私も

uvc.NavigationController.PushViewController(uvc, true); 

同じ問題を試してみました。私はストーリーボードにナビゲーションコントローラを持っていて、あるUIViewから別のUIViewに行くときに画面間のナビゲーションがうまくいっています。

私は一度に私のコード1行を通じて強化していると、それは間違いなくヌル

任意の助けをいただければ幸いですNaviagtionControllerです。

ありがとうございました!

答えて

1

RootViewControllerUINavigationViewcontrollerである必要があります。 AppDelgateでは、UINavigationViewControllerのインスタンスを作成し、それにInitialViewControllerInstanceを渡す必要があります。そして、以下のようなように、1つの変数(navController)にUINavigationControllerのインスタンスを格納します。

AppDelegate.cs

public UINavigationController navController; 

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) 
    { 
     //Here you need to instantiate your first ViewController instance from the storyboard and pass as an argument to the UINavigationController 
     navController = new UINavigationController(yourInitialViewControllerInstance); 
     Window = new UIWindow(UIScreen.MainScreen.Bounds); 
     Window.RootViewController = navController; 
     Window.MakeKeyAndVisible(); 

     return true; 
    } 

を次にあなたが、他のクラスから、他のビューコントローラにあなたをナビゲートしたい場合単にAppDelgateからnavControllerオブジェクトのインスタンスにアクセスし、下記のようにナビゲートします:

void RefreshScreen(NSTimer obj) 
{ 
     Debug.WriteLine("Elapsed");      
     UIStoryboard StoryBoard = UIStoryboard.FromName("Main", null); 
     ViewController uvc = StoryBoard.InstantiateViewController("StartScreenController") as ViewController; 

    //Navigate using the navController Instance from the appDelgate 
    ((AppDelegate)UIApplication.SharedApplication.Delegate).navController.PushViewController(uvc, true); 
} 
+0

はどうもありがとうございました! – Numli

関連する問題