2011-11-07 33 views
1

iOS5中的新風格的應用程序,你必須在UIWindow上設置RootViewController,這讓我非常困惑。MonoTouch和「應用程序在應用程序啓動結束時應該有一個根視圖控制器」錯誤

我從這裏下載最新的MonoTouch.Dialog庫:

https://github.com/migueldeicaza/MonoTouch.Dialog

但是,當我試圖編譯並在模擬器上運行附帶的「樣本」的項目,它崩潰並返回以下錯誤:

Error: Applications are expected to have a root view controller at the end of application launch

我再開GitHub上的問題:

https://github.com/migueldeicaza/MonoTouch.Dialog/issues/65

但米格爾回答我說:

If you use the new style of applications in iOS5, you must set the RootViewController on the UIWindow. That is a new iOS 5 feature part of the cleanup to bring UIViewController containing into place.

我試圖示例應用程序的窗口根視圖控制器的導航控制器分配,但沒有效果。仍然得到相同的錯誤。這是所包含的樣本應用程序的FinishedLaunching方法:

public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
    { 
     var Last = new DateTime (2010, 10, 7); 
     Console.WriteLine (Last); 

     var p = Path.GetFullPath ("background.png"); 
     window.AddSubview (navigation.View); 

     //ADDING THE Navigation Controller as RootViewController 
     window.RootViewController = navigation; //THIS LINE WAS ADDED BY ME 

     var menu = new RootElement ("Demos"){ 
      new Section ("Element API"){ 
       new StringElement ("iPhone Settings Sample", DemoElementApi), 
       new StringElement ("Dynamically load data", DemoDynamic), 
       new StringElement ("Add/Remove demo", DemoAddRemove), 
       new StringElement ("Assorted cells", DemoDate), 
       new StyledStringElement ("Styled Elements", DemoStyled) { BackgroundUri = new Uri ("file://" + p) }, 
       new StringElement ("Load More Sample", DemoLoadMore), 
       new StringElement ("Row Editing Support", DemoEditing), 
       new StringElement ("Advanced Editing Support", DemoAdvancedEditing), 
       new StringElement ("Owner Drawn Element", DemoOwnerDrawnElement), 
      }, 
      new Section ("Container features"){ 
       new StringElement ("Pull to Refresh", DemoRefresh), 
       new StringElement ("Headers and Footers", DemoHeadersFooters), 
       new StringElement ("Root Style", DemoContainerStyle), 
       new StringElement ("Index sample", DemoIndex), 
      }, 
      new Section ("Auto-mapped", footer){ 
       new StringElement ("Reflection API", DemoReflectionApi) 
      }, 
     }; 

     var dv = new DialogViewController (menu) { 
      Autorotate = true 
     }; 
     navigation.PushViewController (dv, true);    

     window.MakeKeyAndVisible(); 

     return true; 
    } 

我所添加的代碼的唯一行是一個在評論中指出的,但這除了似乎並沒有解決錯誤。有什麼我失蹤?

在此先感謝!

+0

你意味着你不能使用MT5編譯MT.D(形成GIT)並使其在iOS(5)模擬器上工作?這就是爲什麼你要添加這一行代碼? – poupou

+0

是的。我正在嘗試使用MonoTouch 5.0編譯MonoTouch.Dialog示例項目。但是我得到了一個崩潰和那個錯誤。有沒有人試圖用MonoTouch 5.0進行編譯? –

+0

我得到這個錯誤,以及我正在做的MonoGame項目(因爲視圖/控制器都隱藏在MonoGame中)。但是,沒有任何崩潰,我只是在控制檯上收到「警告」消息。你確定它不會在別的地方崩潰嗎? – jonathanpeppers

回答

5

我已經更新了MonoTouch.Dialog中的示例以顯示如何將視圖控制器添加到iOS 4.x系統和5.x系統,並且應該修復此問題。

短的版本是,window.AddSubview(navigation.View)是做事的的iOS 4.3的方式,用新的版本中,你需要設置window.RootViewController屬性,像這樣:

 if (UIDevice.CurrentDevice.CheckSystemVersion (5, 0)) 
      window.RootViewController = navigation; 
     else 
      window.AddSubview (navigation.View);    
0

嘗試刪除window.AddSubview(navigation.View);行。

+0

試過了,但我仍然得到相同的錯誤。 –

1

好吧,似乎米格爾上傳了一個新版本的庫。他評論了「更新MonoDevelop 2.8」的提交。

https://github.com/migueldeicaza/MonoTouch.Dialog/commit/25974c5c28d31c022d232a449ef9fbc766506701

現在樣本工作正常(你仍然需要手動設置主窗口的主界面在Info.plist文件,使錯誤消失。最後一次是不夠的。)。

看來問題出在項目設置,而不是在rootviewcontroller的東西。即使沒有最後一個,它也可以正常工作(也許更多的專家可以解釋這個奇怪的事情)。不幸的是,MonoDevelop的錯誤信息是誤導性的!

+0

感謝您解釋它,通過更改項目文件並設置主界面文件爲我工作。 –

相關問題