2012-02-12 65 views
1

我有一個小測試應用程序,只是在3頁之間循環。 這裏的AppDelegate:MonoTouch.Dialog崩潰

public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
    { 
     _session = new Session(); 
     _session.NextScreen += (screenIndex) => 
     { 
      window.RootViewController = _viewControllers[screenIndex]; 
     }; 

     _viewControllers.Add(new Screen0(_session)); 
     _viewControllers.Add(new Screen1(_session)); 
     _viewControllers.Add(new Screen2(_session)); 

     // create a new window instance based on the screen size 
     window = new UIWindow (UIScreen.MainScreen.Bounds); 

     // If you have defined a view, add it here: 
     // window.AddSubview (navigationController.View); 
     window.RootViewController = _viewControllers[0]; 

     // make the window visible 
     window.MakeKeyAndVisible(); 

     return true; 

如果我把每一個畫面,我可以從頁面瀏覽頁面,即上一個按鈕,

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

     UIButton button = new UIButton(new RectangleF(30, 200, 80, 34)); 
     button.SetTitle("Go to 1", UIControlState.Normal); 
     button.TouchUpInside += (sender, e) => 
     { 
      _session.ExittingScreen = 0; 
     }; 
     View.AddSubview(button); 
    } 

然而,當我使用MonoTouch.Dialog,我得到間斷的崩潰。這裏是我的代碼:

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

     var rootElement = new RootElement("Register") 
     { 
      new Section() 
      { 
       new EntryElement("First Name", "required", ""), 
       new EntryElement("Last Name", "required", ""), 
       new EntryElement("Email Address", "required", ""), 
       new EntryElement("Password", "required", "") 
      }, 
      new Section() 
      { 
       new StyledStringElement("Submit you information",() => { _session.ExittingScreen = 1; }) 
      } 
     }; 
     var dialogViewController = new DialogViewController(rootElement); 
     var navigationController = new UINavigationController(dialogViewController); 

     View.Add (navigationController.View); 

和轉儲:

在(包裝託管到本機)MonoTouch.UIKit.UIApplication.UIApplicationMain(整型,字符串[],IntPtr的,IntPtr的)<爲0xffffffff > 在MonoTouch.UIKit.UIApplication.Main(串[],字符串,字符串)< 0x000b7> 在MTD.Application.Main(串[])< 0x00017> 在(包裝紙運行時調用).runtime_invoke_void_object(對象, intptr,intptr,intptr)< 0xffffffff>

母語堆棧跟蹤:

0 MTD         0x00090b7c mono_handle_native_sigsegv + 284 
1 MTD         0x00005f28 mono_sigsegv_signal_handler + 248 
2 libsystem_c.dylib     0x97da559b _sigtramp + 43 
3 ???         0xffffffff 0x0 + 4294967295 
4 UIKit        0x02220952 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 201 
5 Foundation       0x0173786d __NSFireDelayedPerform + 389 
6 CoreFoundation      0x01195966 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22 
7 CoreFoundation      0x01195407 __CFRunLoopDoTimer + 551 
8 CoreFoundation      0x010f87c0 __CFRunLoopRun + 1888 
9 CoreFoundation      0x010f7db4 CFRunLoopRunSpecific + 212 
10 CoreFoundation      0x010f7ccb CFRunLoopRunInMode + 123 
11 GraphicsServices     0x04789879 GSEventRunModal + 207 
12 GraphicsServices     0x0478993e GSEventRun + 114 
13 UIKit        0x02190a9b UIApplicationMain + 1175 
14 ???         0x09ff6774 0x0 + 167733108 
15 ???         0x09ff5958 0x0 + 167729496 
16 ???         0x09ff57f0 0x0 + 167729136 
17 ???         0x09ff587f 0x0 + 167729279 
18 MTD         0x0000a292 mono_jit_runtime_invoke + 722 
19 MTD         0x0016a17e mono_runtime_invoke + 126 
20 MTD         0x0016e264 mono_runtime_exec_main + 420 
21 MTD         0x00173685 mono_runtime_run_main + 725 
22 MTD         0x00067495 mono_jit_exec + 149 
23 MTD         0x002116c9 main + 2825 
24 MTD         0x000032e5 start + 53 

25 ??? 0x00000005 0x0 + 5

我做錯了什麼,或者這是一個錯誤?謝謝。

+0

只是爲了澄清,當我觸摸「提交」SyledStringElement – user1205691 2012-02-12 23:54:29

回答

0

避免模式,如:

var navigationController = new UINavigationController(dialogViewController); 
View.Add (navigationController.View); 

因爲navigationController一旦View.Add調用完成,垃圾收集器可以配置它(只要它想)將不會被引用(在管理側)。然而從本地方面,它將預計存在。調用返回(從本機管理)到處置的實例將崩潰您的應用程序。

正確的模式是將navigationController聲明爲您的類型的字段(而不是局部變量)並在方法中創建/分配它。只要父實例存在(並確保任何回調不會到達處置的對象),這將保持navigationController活動的引用。

E.g.

private UINavigationController navigationController; 
... 
public override void ViewDidLoad() 
{ 
    ... 
    var dialogViewController = new DialogViewController(rootElement); 
    navigationController = new UINavigationController(dialogViewController); 

    View.Add (navigationController.View); 
    ... 
} 
+0

謝謝。這似乎阻止了間歇性的崩潰。 – user1205691 2012-02-13 20:51:57

+1

好消息:-)行,如'3 ??? 0xffffffff 0x0 + 4294967295'通常指向一個不存在的實例(當從本地框架調用時更多)。 – poupou 2012-02-13 21:23:04