2013-04-04 17 views
2

編輯:我現在得到的異常:使用AddTarget爲的UIButton與MonoTouch的和C#

Stacktrace: 

    at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff> 
    at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
    at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:43 
    at MapKit01.Application.Main (string[]) [0x00000] in /Users/andre/Downloads/MapKit03/MapKit03/Main.cs:20 
    at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff> 

Native stacktrace: 

0 MapKit01       0x00091eac mono_handle_native_sigsegv + 284 
1 MapKit01       0x00005788 mono_sigsegv_signal_handler + 248 
2 libsystem_c.dylib     0x9533f8cb _sigtramp + 43 
3 ???         0xffffffff 0x0 + 4294967295 
4 UIKit        0x0274f258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 
5 UIKit        0x02810021 -[UIControl sendAction:to:forEvent:] + 66 
6 UIKit        0x0281057f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578 
7 UIKit        0x0280f6e8 -[UIControl touchesEnded:withEvent:] + 546 
8 UIKit        0x0277ecef -[UIWindow _sendTouchesForEvent:] + 846 
9 UIKit        0x0277ef02 -[UIWindow sendEvent:] + 273 
10 UIKit        0x0275cd4a -[UIApplication sendEvent:] + 436 
11 UIKit        0x0274e698 _UIApplicationHandleEvent + 9874 
12 GraphicsServices     0x04d40df9 _PurpleEventCallback + 339 
13 GraphicsServices     0x04d40ad0 PurpleEventCallback + 46 
14 CoreFoundation      0x012bfbf5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53 
15 CoreFoundation      0x012bf962 __CFRunLoopDoSource1 + 146 
16 CoreFoundation      0x012f0bb6 __CFRunLoopRun + 2118 
17 CoreFoundation      0x012eff44 CFRunLoopRunSpecific + 276 
18 CoreFoundation      0x012efe1b CFRunLoopRunInMode + 123 
19 GraphicsServices     0x04d3f7e3 GSEventRunModal + 88 
20 GraphicsServices     0x04d3f668 GSEventRun + 104 
21 UIKit        0x0274bffc UIApplicationMain + 1211 
22 ???         0x0e0c66ad 0x0 + 235693741 
23 ???         0x0e0c61b0 0x0 + 235692464 
24 ???         0x0e0c5f0c 0x0 + 235691788 
25 ???         0x0e0c5ccc 0x0 + 235691212 
26 ???         0x0e0c5eb6 0x0 + 235691702 
27 MapKit01       0x00009b52 mono_jit_runtime_invoke + 722 
28 MapKit01       0x0016d02e mono_runtime_invoke + 126 
29 MapKit01       0x00171224 mono_runtime_exec_main + 420 
30 MapKit01       0x00176615 mono_runtime_run_main + 725 
31 MapKit01       0x000671e5 mono_jit_exec + 149 
32 MapKit01       0x00204fd4 main + 1988 
33 MapKit01       0x00002b75 start + 53 
34 ???         0x00000004 0x0 + 4 

================================================================= 
Got a SIGSEGV while executing native code. This usually indicates 
a fatal error in the mono runtime or one of the native libraries 
used by your application. 
================================================================= 

問題: 我喜歡使用一些代碼,我在Objective-C中發現在我的C#項目。 它工作正常,但使用addTarget行讓我覺得自從幾個小時沒有工作結果。 因此,這是原始來源:

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

而且這是我嘗試:

UIButton infoButton = new UIButton(UIButtonType.DetailDisclosure); 
    infoButton.AddTarget(infoButton, new MonoTouch.ObjCRuntime.Selector("showDetails"),UIControlEvent.TouchUpInside); 

    public void showDetails(object sender, EventArgs e) 

結果是不是很好的應用程序崩潰,而不當我點擊按鈕的任何反應.. 。

請問如何在C#中以正確的方式使用它?

感謝

安德烈

回答

5

嘗試使用C#事件是這樣的:

infoButton.TouchUpInside += showDetails; 
+0

羅爾夫感謝,但我得到上述的異常。 – 2013-04-04 17:41:26

+1

您需要將按鈕存儲在類級別的變量中,而不是隻將它放在方法中。 – 2013-04-04 21:15:59

+1

必須將按鈕保存在類級別變量中的原因是,否則垃圾收集器將會銷燬它,這就是爲什麼你會得到錯誤。 – 2013-04-05 15:23:33

相關問題