使用AddTarget为UIButton与MonoTouch和C#

时间:2013-04-04 14:01:30

标签: c# objective-c xcode xamarin.ios

编辑:我现在得到例外:

 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.
=================================================================

问题: 我喜欢在我的C#项目中使用我在Objective-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#中使用它吗?

由于

安德烈

1 个答案:

答案 0 :(得分:5)

尝试使用这样的C#事件:

infoButton.TouchUpInside += showDetails;